亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

介紹:

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

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

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

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

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

使用Selenium:

1、我們新建一個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這個頁面來演示。

頁面源碼如下

<!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)滑動驗證碼的驗證)

//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#/VB.NET?將Word與Excel文檔轉(zhuǎn)化為Text

    C#/VB.NET?將Word與Excel文檔轉(zhuǎn)化為Text

    這篇文章主要介紹了C#/VB.NET?將Word與Excel文檔轉(zhuǎn)化為Text,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-08-08
  • C# String字符串案例詳解

    C# String字符串案例詳解

    這篇文章主要介紹了C# String字符串案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Unity3D中腳本的執(zhí)行順序和編譯順序

    Unity3D中腳本的執(zhí)行順序和編譯順序

    在Unity中可以同時創(chuàng)建很多腳本,并且可以分別綁定到不同的游戲?qū)ο笊希鼈兏髯远荚谧约旱纳芷谥羞\(yùn)行。與腳本有關(guān)的也就是編譯和執(zhí)行啦,本文就來研究一下Unity中腳本的編譯和執(zhí)行順序的問題。
    2014-11-11
  • HTML文本框的值改變后觸發(fā)后臺代碼的方法

    HTML文本框的值改變后觸發(fā)后臺代碼的方法

    asp.net用日期插件,當(dāng)選中一個日期時觸發(fā)一個事件,以查詢當(dāng)前日期的數(shù)據(jù)。這是要跟數(shù)據(jù)庫交互的。先貼出控件代碼:
    2013-04-04
  • Unity3D生成一段隧道網(wǎng)格的方法

    Unity3D生成一段隧道網(wǎng)格的方法

    這篇文章主要為大家詳細(xì)介紹了Unity3D生成一段隧道網(wǎng)格的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • C#中ManualResetEvent用法總結(jié)

    C#中ManualResetEvent用法總結(jié)

    這篇文章主要介紹了C#中ManualResetEvent用法總結(jié),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-01-01
  • c#數(shù)據(jù)庫與TXT導(dǎo)入導(dǎo)出的實(shí)例

    c#數(shù)據(jù)庫與TXT導(dǎo)入導(dǎo)出的實(shí)例

    最近剛學(xué)完ADO.NET,做了個數(shù)據(jù)導(dǎo)入導(dǎo)出的題目,是將txt中的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,然后將數(shù)據(jù)庫中的數(shù)據(jù)導(dǎo)出到txt中,這里說的數(shù)據(jù)的格式是“tom|23”,tom指名字,23指年齡。廢話也不多說了,大家直接看代碼。
    2013-04-04
  • C# SQlite操作方法小結(jié)

    C# SQlite操作方法小結(jié)

    這篇文章主要介紹了C# SQlite操作方法,較為詳細(xì)的講述了SQLite的下載與C#針對SQLite的連接、創(chuàng)建、與執(zhí)行SQL語句等操作相關(guān)技巧,需要的朋友可以參考下
    2016-07-07
  • VS2015 C#生成dll文件的方法(32/64)

    VS2015 C#生成dll文件的方法(32/64)

    這篇文章主要介紹了VS2015 C#生成dll文件的方法(32/64),需要的朋友可以參考下
    2016-12-12
  • C#獲取存儲過程返回值和輸出參數(shù)值的方法

    C#獲取存儲過程返回值和輸出參數(shù)值的方法

    這篇文章主要介紹了C#獲取存儲過程返回值和輸出參數(shù)值的方法,有需要的朋友可以參考一下
    2014-01-01

最新評論