Selenium Webdriver Study Notes

Source: Internet
Author: User

1. Open VS2012

2. New Project (Unit test project or console program can be, see requirements)

3. Tool->nuget Package Manager Package Manager console

4. Enter the "Install-package selenium.webdriver" installer package

5. Enter the "Install-package selenium.webdriverbackedselenium" installer package

6. Enter the "Install-package selenium.support" installer package

The meaning of some methods (subjective sense, limited documentation (and no domestic access), a day or so of contact with time):

//Firefox controller, there are other browsers, need to install the corresponding driver, such as ChromedriverIwebdriver Driver =Newfirefoxdriver ();//Load PageDriver. Navigate (). Gotourl (BaseURL +"/");//Maximize Windowdriver. Manage (). Window.maximize ();//Thread WaitsSystem.Threading.Thread.Sleep ( +);//click on the elementDriver. Findelement (By.id ("Login")). Click ();//fill out the formDriver. Findelement (By.id ("Poploginid")). SendKeys ("xachary");//Get mouse ActionActions actions =NewActions (driver);//move the mouse to the elementactions. Movetoelement (element);//move coordinates (based on the previous relative position)Actions. Movebyoffset (0, -);//perform mouse action (with multiple actions)actions. Perform ();//wait for the element to appear (the key is to wait for the element to be visible from invisible, otherwise invalid, then use Thread.Sleep bar)Driver. Manage (). Timeouts (). Implicitlywait (NewTimeSpan (10000* -));//find elements with CSS selectionDriver. Findelement (By.cssselector ("li.first > A > IMG")). Click ();
View Code

Example fragment:

usingOpenqa.selenium;usingOpenQA.Selenium.Support.UI;usingOpenQA.Selenium.Firefox;usingOpenQA.Selenium.Interactions;usingOpenQA.Selenium.Interactions.Internal;usingSystem.Drawing;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingOpenQA.Selenium.Chrome;namespacebosseleniumdriver{classProgram {Static voidMain (string[] args)            {Iwebdriver driver; stringBaseURL; Driver=NewFirefoxdriver (); //Driver = new Chromedriver (); //BaseURL = "http://localhost: 49912 ";BaseURL ="http://172.16.8.7:4399"; Try            {                //Home PageDriver. Navigate (). Gotourl (BaseURL +"/"); //Maximize Windowdriver. Manage ().                Window.maximize (); //thread waits 1 secondsSystem.Threading.Thread.Sleep ( +); //LoginDriver. Findelement (By.id ("Login")).                Click (); Driver. Findelement (By.id ("Poploginid")). SendKeys ("xachary"); Driver. Findelement (By.id ("Password")). SendKeys ("home890619"); Driver. Findelement (By.id ("loginbtn")).                Click (); //thread waits 1 secondsSystem.Threading.Thread.Sleep ( +); Actions Actions=NewActions (driver); //move the mouse to make the navigation appearIwebelement nav = driver. Findelements (By.cssselector (". Has_sub"))[0]; Actions.                Movetoelement (NAV); Actions. Movebyoffset (0, -); Actions.                Perform (); //wait for navigation to appear, 100 millisecondsDriver. Manage (). Timeouts (). Implicitlywait (NewTimeSpan (10000* -)); //Click the Family Medicine Kit Linkactions.                Click (); Actions.                Perform (); //Select the first itemDriver. Findelement (By.cssselector ("li.first > A > IMG")).                Click (); //jump to a new pagedriver. SwitchTo (). Window (driver.                Windowhandles.last ()); //buyDriver. Findelement (By.linktext ("Buy Now")).                Click (); //waiting to get the delivery addressDriver. Manage (). Timeouts (). Implicitlywait (NewTimeSpan (0,0,3)); //If there is no address                if(Driver. Findelements (By.cssselector (". Address-radio.fl")). Count = =0)                {                    //cascading drop-down box                    NewSelectElement (driver. Findelement (By.id ("sltprovince"))). Selectbytext ("Guangdong province"); Driver. Manage (). Timeouts (). Implicitlywait (NewTimeSpan (0,0,3)); NewSelectElement (driver. Findelement (By.id ("sltcity"))). Selectbytext ("Zhuhai"); Driver. Manage (). Timeouts (). Implicitlywait (NewTimeSpan (0,0,3)); NewSelectElement (driver. Findelement (By.id ("Sltarea"))). Selectbytext ("Xiangzhou District"); //fill out the formDriver. Findelement (By.id ("txtaddress")).                    Clear (); Driver. Findelement (By.id ("txtaddress")). SendKeys ("Test Address"); Driver. Findelement (By.id ("txtreceive")).                    Clear (); Driver. Findelement (By.id ("txtreceive")). SendKeys ("Test Consignee"); Driver. Findelement (By.id ("Txttelephone")).                    Clear (); Driver. Findelement (By.id ("Txttelephone")). SendKeys ("18507567676"); //Submit FormDriver. Findelement (By.id ("btnupdateaddr")).                Click (); } driver. Findelement (By.name ("rdoaddr")).                Click (); //waiting for product information to appearwebdriverwait wait =NewWebdriverwait (Driver,NewTimeSpan (0,0,3)); Wait. Until<IWebElement> ((o) =                {                    returnDriver. Findelement (By.cssselector (". CART_LIST_TD TR"));                }); //wait for the Submit order button to take effectSystem.Threading.Thread.Sleep ( -); //Submit OrderDriver. Findelement (By.linktext ("Submit Order")).                Click (); //choose ICBC to payWebdriverwait Wait2 =NewWebdriverwait (Driver,NewTimeSpan (0,0,5)); Wait2. Until<IWebElement> ((o) =                {                    returnDriver. Findelement (By.id ("GH")); }).                Click (); //PaymentDriver. Findelement (By.id ("Btn_pay")).                Click (); Driver. Manage (). Timeouts (). Implicitlywait (NewTimeSpan (0,0,2)); //Turn off Payment pop-up pagedriver. SwitchTo (). Window (driver.                Windowhandles.last ()); Driver.                Close (); //back to Payment pagedriver. SwitchTo (). Window (driver.                Windowhandles.last ()); Driver. Findelement (By.linktext ("payment encountered a problem")).            Click (); }            Catch(Exception ex) {Console.WriteLine (ex).            Message); }            finally{Console.WriteLine ("End"); System.Threading.Thread.Sleep ( the); //console.readline ();driver.            Quit (); }        }    }}
View Code

Selenium Webdriver Learning notes

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.