最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用Selenium的實(shí)現(xiàn)代碼

 更新時(shí)間:2020年03月18日 10:09:41   作者:zhaotianff  
這篇文章主要介紹了C#使用Selenium的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

介紹:

Selenium 是一個(gè)用于Web應(yīng)用程序測試的工具。Selenium測試直接運(yùn)行在瀏覽器中,就像真正的用戶在操作一樣。支持的瀏覽器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等。

利用它可以驅(qū)動瀏覽器執(zhí)行特定的動作,如點(diǎn)擊、下拉等操作,同時(shí)還可以獲取瀏覽器當(dāng)前呈現(xiàn)的頁面的源代碼 ,做到可見即可爬。

所以Selenium現(xiàn)在被廣泛用于Python爬蟲。查了下資料,發(fā)現(xiàn)這個(gè)工具確實(shí)強(qiáng)大,最重要的是,C#也是可以調(diào)用的。

官方支持Java,C#,Python,Ruby,PHP,Perl,Javascript等語言

Selenium使用Java開發(fā),項(xiàng)目地址https://github.com/SeleniumHQ/selenium

使用Selenium:

1、我們新建一個(gè)C#控制臺程序

2、使用Nuget搜索以下依賴庫

需要引用的核心庫是Selenium.RC,Selenium.Support,Selenium.WebDriver

然后再需要引用 瀏覽器驅(qū)動庫,這里我以IE瀏覽器為例,Chrome使用方式跟IE是一樣的,程序包名稱為Selenium.WebDriver.ChromeDriver。

3、在Main函數(shù)中輸入以下代碼

static void Main(string[] args)
    {
      using (IWebDriver driver = new OpenQA.Selenium.IE.InternetExplorerDriver())
      {
        driver.Navigate().GoToUrl("http://www.baidu.com"); //driver.Url = "http://www.baidu.com"是一樣的

        var source = driver.PageSource;

        Console.WriteLine(source);
      }
    }

運(yùn)行,會彈出IE瀏覽器,網(wǎng)頁加載完成后,瀏覽器會自動關(guān)閉??刂婆_輸入結(jié)果如下

這樣我們就可以輕松的獲取動態(tài)渲染頁面的源碼。

基本用法:

這里我以https://technet-info.com/Main.aspx這個(gè)頁面來演示。

頁面源碼如下

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="description" content="Wandering the number of windows, stayed in the number of hotels, will feel that separation is not wronged, the feelings are used to browse or used to collect, so that the day had a memorable day" /><title>
  Welcome To Technet-Info : Personal Gallery
</title><link rel="shortcut icon" type="image/x-icon" href="technet.ico" rel="external nofollow" media="screen" /><link rel="stylesheet" href="Css/MainCss.css" rel="external nofollow" /><link rel="stylesheet" href="Css/screen.css" rel="external nofollow" />
  <style>
    #footer{
      display: flex;
      justify-content: center;
      align-items: center;
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
    }
  </style>
  <script type="text/javascript" src="js/jquery.js"></script>
  <script type="text/javascript" src="js/easySlider1.7.js"></script>  
  <script type="text/javascript">
    $(document).ready(function () {
      $("#slider").easySlider({
        auto: true,
        pause:3000,
        continuous: true,
        numeric: true
      });
    });   
  </script>
</head>
<body>
  <form method="post" action="./Main.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTQyNjI2MTkwNmRkt331eyucv2SBluj0E2d+0haGV4exFHWtGQkZhNBnpHE=" />
</div>

<div class="aspNetHidden">

  <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="202EA31B" />
</div>
    <div id="main">
      <div id="header">
        <div class="musicarea">
          
          <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=150 height=52 src="http://music.163.com/outchain/player?type=0&id=516657278&auto=1&height=32"></iframe>
        </div>
        <div class="content">
          
          <div class="logo">
            
            <div class="logo_img">
              <div class="logo_img"></div>
            </div>
            
            <div class="logo_txt">
              <div style="height: 50px;">
                <p></p>
              </div>
              <div style="height: 50px;">
                <p>我的freetime</p>
              </div>
            </div>
          </div>


          
          <div class="menu">
            
        </div>
      </div>
      
      <div id="content">
        
        
        </div>
                
        <div id="cards">
            
          </div>
        <div id="pin">
          
        </div>

      </div>
      
      <div id="footer">
        <div id="copyright">
          <p style="margin: 3px">
            <a  rel="external nofollow" >湘ICP備16012349號</a>
            <span>|</span>
            <span>Copyright © 2016, www.technet-info.com, All rights reserved.</span>
          </p>
          <p><a href="mailto:zhaotianff@163.com" rel="external nofollow" >Email:zhaotianff@163.com</a></p>
        </div>
      </div>
    </div>
  </form>
</body>
</html>

通過id獲取元素

//by id
var byID = driver.FindElement(By.Id("cards"));

通過類名獲取元素

//by class name
var byClassName = driver.FindElements(By.ClassName("menu"));

通過標(biāo)簽名獲取元素

//by tag name 
var byTagName = driver.FindElement(By.TagName("iframe"));

通過名字獲取元素

var byName = driver.FindElement(By.Name("__VIEWSTATE"));

通過鏈接文本獲取元素

//by linked text 
//<a  rel="external nofollow" rel="external nofollow" >linkedtext</a>> 
var byLinkText = driver.FindElement(By.LinkText("linkedtext"));

通過部分鏈接文本獲取元素

//by partial link text
//<a  rel="external nofollow" rel="external nofollow" >linkedtext</a>>
var byPartialLinkText = driver.FindElement(By.PartialLinkText("text"));

通過CSS選擇器獲取元素

//by css
var byCss = driver.FindElement(By.CssSelector("#header .content .logo"));

通過XPath來獲取元素(XPath使用可以參考上一篇博客)

//by xpath
var byXPath = driver.FindElements(By.XPath("http://div"));

執(zhí)行JS

//execute javascript
var jsReturnValue = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("jsfunname");

獲取元素的值和屬性

//get element value and attribute value
var byIDText = byID.Text;
var byIDAttributeText = byID.GetAttribute("id");

模擬鼠標(biāo)點(diǎn)擊元素

//click
driver.FindElement(By.Id("copyright")).Click();

頁面導(dǎo)航

//Navigation
driver.Navigate().Forward();
driver.Navigate().Back();

拖拽操作(可以實(shí)現(xiàn)滑動驗(yàn)證碼的驗(yàn)證)

//Drag And Drop
var element = driver.FindElement(By.Name("source"));
IWebElement target = driver.FindElement(By.Name("target"));
(new Actions(driver)).DragAndDrop(element, target).Perform();

示例代碼

到此這篇關(guān)于C#使用Selenium的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)C#使用Selenium內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C# salt+hash 加密

    C# salt+hash 加密

    本文主要介紹了C# salt+hash加密規(guī)則、C# salt產(chǎn)生偽隨機(jī)數(shù)原理、hash原理、使用hash來加密的原因等等。具有一定的參考價(jià)值,下面跟著小編一起來看下吧
    2017-01-01
  • c# datetime 格式化大全

    c# datetime 格式化大全

    這篇文章主要介紹了c# datetime 格式化大全,有需要的朋友可以參考一下
    2014-01-01
  • C# LINQ to XML應(yīng)用介紹

    C# LINQ to XML應(yīng)用介紹

    .Net又引入了LINQ,于是LINQ to XML也就應(yīng)運(yùn)而生,所以在.Net中,不僅可以用W3C XML DOM標(biāo)準(zhǔn),還可以使用LINQ to XML來操作XML文檔。下面就來簡單介紹一下如何使用LINQ to XML
    2012-11-11
  • C#多線程處理多個(gè)隊(duì)列數(shù)據(jù)的方法

    C#多線程處理多個(gè)隊(duì)列數(shù)據(jù)的方法

    本文將結(jié)合實(shí)例代碼,介紹C#多線程處理多個(gè)隊(duì)列數(shù)據(jù)的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • .Net Winform開發(fā)筆記(一)

    .Net Winform開發(fā)筆記(一)

    理解“Windows 窗體應(yīng)用程序”項(xiàng)目中Program.cs文件中的main方法與傳統(tǒng)C++Console控制臺程序中的main方法的區(qū)別等等,感興趣的朋友可以了解下
    2013-01-01
  • C#圖像灰度級拉伸的方法

    C#圖像灰度級拉伸的方法

    這篇文章主要介紹了C#圖像灰度級拉伸的方法,涉及C#灰度操作的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • c# 根據(jù)NPOI 讀取一個(gè)excel 文件的多個(gè)Sheet

    c# 根據(jù)NPOI 讀取一個(gè)excel 文件的多個(gè)Sheet

    這篇文章主要介紹了c# 根據(jù)NPOI 讀取一個(gè)excel 文件的多個(gè)Sheet,幫助大家更好的利用c#處理excel表格,感興趣的朋友可以了解下
    2020-12-12
  • c# 實(shí)現(xiàn)的支付寶支付

    c# 實(shí)現(xiàn)的支付寶支付

    這篇文章主要介紹了c# 實(shí)現(xiàn)的支付寶支付的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-01-01
  • Unity?UGUI的StandaloneInputModule標(biāo)準(zhǔn)輸入模塊組件使用示例

    Unity?UGUI的StandaloneInputModule標(biāo)準(zhǔn)輸入模塊組件使用示例

    這篇文章主要為大家介紹了Unity?UGUI的StandaloneInputModule標(biāo)準(zhǔn)輸入模塊組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • C#操作注冊表之Registry類

    C#操作注冊表之Registry類

    這篇文章介紹了C#操作注冊表之Registry類,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評論

沽源县| 兴安盟| 屯门区| 靖安县| 丰都县| 商都县| 芷江| 新密市| 芒康县| 保亭| 阿鲁科尔沁旗| 教育| 巧家县| 蚌埠市| 无棣县| 普定县| 江川县| 巧家县| 郓城县| 叶城县| 永和县| 航空| 怀集县| 滁州市| 元氏县| 佛学| 固镇县| 昌邑市| 增城市| 和平区| 句容市| 益阳市| 佛山市| 璧山县| 乌海市| 绥化市| 禄丰县| 华阴市| 嘉禾县| 宿松县| 乌兰察布市|