(GO). NET SELENIUM2 Usage Summary

Source: Internet
Author: User
Tags tagname

I. Introduction of Selenium
1.SELENIUM1 (Selenium RC) Selenium2 (Selenium webdriver) Selenium2.0 = Selenium1.0 + Webdriver (that is, Selenium2.0 merges these two items)
2.WebDriver support for Firefox (Firefoxdriver), IE (Internetexplorerdriver), Opera (Operadriver) and Chrome (Chromedriver)

Ii. summary of daily use methods
1. Build a Web Object
Iwebdriver driver;
Driver = new Firefoxdriver ();

2. Jump to the specified page
Driver. Navigate (). Gotourl (BaseURL + "/");
Driver.title gets the title of the current page
Driver.url gets the URL of the current page

3. Execute JS Script
((Ijavascriptexecutor) driver). Executescript ("JS")

4. Positioning elements
Driver. Findelement (By.id ("cp1_btnmodify"))

    by.classname (ClassName))      
         by.cssselector (selector)         
         by.id (Id)                        
        By.linktext (LinkText)            
         by.name (Name)                
        by.partiallinktext (linkText)  
         by.tagname (name)         
         By.xpath (XPathExpression)

5. Positioning elements in frame
(1) Enter frame
Driver. SwitchTo (). FRAME ("frame");

(2) Positioning elements
Driver. Findelement (By.id ("Div1"));
Driver. Findelement (By.id ("input1"));

(3) Exit frame
Driver. SwitchTo (). Defaultcontent ();

6. How to get a popup window
(1) Handle to the current window
Driver. Currentwindowhandle

(2) Handle of all windows
Driver. Windowhandles

(3) Get the window according to the handle
If driver. Currentwindowhandle=driver. Windowhandles[i]
Iwebdriver Window=driver. SwitchTo (). Window (driver. Windowhandles[i])

(4) According to the window to get Title,url
Window. Title
Window. Url

7. How to handle alert, confirm, prompt dialog boxes
(1) Get alert box information
HTML code:
<input id = "alert" value = "alert" type = "button" onclick = "alert (' Welcome! Please press confirm to continue! ‘);" />
Driver. Findelement (by.id ("alert")). Click ();
Ialert alert = driver. SwitchTo (). Alert ();
Console.WriteLine (Alert. Text);
Confirm. Dismiss (); Point Bounce Box off

(2) Get the text above the output dialog box
HTML code:
<input id = "Confirm" value = "confirm" type = "button" onclick = "confirm (' OK? ‘);" />

Driver. Findelement (By.id ("confirm")). Click ();
Ialert confirm = driver. SwitchTo (). Alert ();
Console.WriteLine (confirm. Text);
Confirm. Accept (); Click OK

(3) Click the button, enter a name, then click Confirm
HTML code:
<input id = "Prompt" value = "prompt" type = "button" onclick = "var name = prompt (' Please enter your name: ', ' Please enter
Your name '); document.write (name) "/>

Driver. Findelement (By.id ("prompt")). Click ();
Ialert prompt = driver. SwitchTo (). Alert ();
Console.WriteLine (Prompt. Text);
Prompt. SendKeys ("Hello");
Prompt. Accept (); Click OK

8. How to handle the Select drop-down box
SelectElement selectcity=new selectelement (driver. Findelement (By.id ("City"));

(1) Select the second item by the index of the drop-down box
Selectcity.selectbyindex (2);
(2) Select by the Value property of the option in the drop-down list
Selectcity.selectbyvalue ("10");

(3) Select the Text property of the option in the drop-down list
Selectcity.selectbytext ("Beijing");

(4) walk through all the options in the drop-down list and click to select the option
foreach (Iwebelement e in selectcity.options)
{
E.click ();
}

9. How to Operate cookies
(1) Add a name = "name", value= "value" of the cookie
Cookie Cookie=new Cookie ("name", "value");
Driver. Manage (). Cookies.addcookie (cookie);

(2) Get all cookies under the page, enter its domain, name, value, valid date, path
Icookiejar Cookies=driver. Manage (). Cookies;
Cookie Co = cookies. Getcookienamed ("name");
Console.WriteLine (Co. Domain);
Console.WriteLine (Co. Name);
Console.WriteLine (Co. Value);
Console.WriteLine (Co. expiry);
Console.WriteLine (Co. Path);
(3) Delete cookies three ways
A) through the name of the cookie
Driver. Manage (). Cookies.deletecookienamed ("CookieName");

b) through the cookie object
Driver. Manage (). Cookies.deletecookie (cookie);

c) Delete all
Driver. Manage (). Cookies.deleteallcookies ();
10. How to wait for page element loading to complete
(1) Clear waiting
(2) Invisible wait

11. How to use Selenium-webdriver
Thread.Sleep (5000);
Screenshot screenshotfile = ((itakesscreenshot) driver). Getscreenshot ();
Screenshotfile.saveasfile ("Test", imageformat.jpeg);

12. How to get the content in table
(1) Ways to get columns by row
Private Iwebelement Getcell (iwebelement row,int cell)
{
Ilist<iwebelement> cells;
Iwebelement target = null;
The column contains "<th>", "<td>" two kinds of labels, so separate processing
if (row. Findelements (By.tagname ("th")). COUNT&GT;0)
{
Cells = row. Findelements (By.tagname ("th"));
target = Cells[cell];
}

if (row. Findelements (By.tagname ("TD")). COUNT>0)
{
Cells = row. Findelements (By.tagname ("TD"));
target = Cells[cell];
}
return target;
}

(2) method of obtaining a line through by
Public String Getcelltext (by by,string tablecelladdress)
{
Get TABLE Element
Iwebelement table = driver. Findelement (by);
The cell position character to be searched is decomposed to get the corresponding row, column
int index = Tablecelladdress.trim (). IndexOf ('. ');
int row = Convert.ToInt32 (tablecelladdress.substring (0, index));
int cell = Convert.ToInt32 (tablecelladdress.substring (index + 1));
Get all the row objects in table tables and get the row objects you want to query
ilist<iwebelement> rows = table. Findelements (By.tagname ("tr"));
Iwebelement therow = Rows[row];
Return Getcell (Therow, cell). Text;
}

(3) The contents of the corresponding rows and columns are obtained by parameters
Console.WriteLine (Getcelltext (By.id ("MyTable"), "0.2")); Get the table contents of the third column in the first row of id= "MyTable"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.