C # implementation code using selenium



Selenium is a tool for testing web applications. Selenium tests run directly in the browser, just like real users. Supported browsers include ie (7, 8, 9, 10, 11), Mozilla, Firefox, Safari, Google Chrome, opera, etc.

It can be used to drive the browser to perform specific actions, such as click, drop-down and other operations. At the same time, it can also obtain the source code of the current page presented by the browser, so that it can be seen and crawled.

So selenium is now widely used in Python crawlers. After checking the data, we found that this tool is really powerful. Most importantly, C # can also be called.

Official support for Java, C #, python, ruby, PHP, Perl, JavaScript and other languages

Selenium using java development, project address https://github.com/SeleniumHQ/selenium

Using selenium:

1. Let’s create a new C console program

2. Use nuget to search the following dependency Libraries

The core libraries that need to be referenced are Selenium.RC , Selenium.Support , Selenium.WebDriver

Then you need to refer to the browser driver library. Here I take IE browser as an example. Chrome is used in the same way as ie, and the package name is Selenium.WebDriver.ChromeDriver .

3. Enter the following code in the main function

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 "It's the same

        var source = driver.PageSource;


Run, will pop up IE browser, web page loading completed, the browser will automatically close. The console input results are as follows

So we can easily get the source code of dynamic rendering page.

Basic usage:

Here I’ll take https://technet-info.com/Main.aspx This page is for demonstration.

The source code of the page is as follows

<!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" />
      display: flex;
      justify-content: center;
      align-items: center;
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
  <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 () {
        auto: true,
        continuous: true,
        numeric: true
  <form method="post" action="./Main.aspx">
<input type="hidden" name="__VIEWSTATE" value="/wEPDwUKLTQyNjI2MTkwNmRkt331eyucv2SBluj0E2d+0haGV4exFHWtGQkZhNBnpHE=" />


  <input type="hidden" name="__VIEWSTATEGENERATOR" value="202EA31B" />
          <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 style="height: 50px;">
              <div style="height: 50px;">
                <p>My freetime</p>


          <p style="margin: 3px">
            <a href=" http://www.miitbeian.gov.cn/ "Rel =" external nofollow "> Xiang ICP no.16012349</a>
            <span>Copyright © 2016, www.technet-info.com, All rights reserved.</span>
          <p><a href="mailto:[email protected]" rel="external nofollow" >Email:[email protected]</a></p>

Getting elements by ID

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

Get elements by class name

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

Get element by tag name

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

Get elements by name

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

Get elements from linked text

//by linked text 
//<a href="http://www.google.com" rel="external nofollow" rel="external nofollow" >linkedtext</a>> 
var byLinkText = driver.FindElement(By.LinkText("linkedtext"));

Get elements from partial linked text

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

Getting elements through CSS selectors

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

Get elements through XPath (for the use of XPath, please refer to the previous blog)

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

Execute JS

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

Gets the values and properties of an element

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

Simulate mouse click elements


Page navigation


Drag and drop operation (can realize the verification of sliding verification code)

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

Sample code

So far, this article about the implementation code of C # using selenium is introduced here. For more related content of C # using selenium, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!