selenium2.0 (Webdriver) API

Source: Internet
Author: User
Tags tagname

1.1 Download Selenium2.0 's Package
    1. Official download package Address: Http://code.google.com/p/selenium/downloads/list
    2. Official User guide:http://seleniumhq.org/docs/
    3. Official api:http://selenium.googlecode.com/git/docs/api/java/index.html
1.2.1 Open a browser with Webdriver
    • Open Firefox Browser:

Webdriver Driver = new Firefoxdriver ();

    • Open IE browser

Webdriver Driver = new Internetexplorerdriver ();

    • Open Htmlunit Browser

Webdriverdriver = new Htmlunitdriver ();

    • Open Chrome Browser

Webdriverdriver = new Chromedriver ();

1.2.2 Maximize Browser

Webdriver Driver = new Firefoxdriver ();
Driver.manage (). window (). Maximize ();

1.2.3 Close Browser

Webdriver Driver = new Firefoxdriver ();

    • Driver.close ();
    • Driver.quit ();
1.3 Opening the test page
    • Driver.get ("http://www.google.com");
    • Driver.navigate (). to ("http://www.baidu.com/");

The P.s.navigate method produces 1 navigator objects that encapsulate some navigation-related methods, such as forward and backward.

1.4 Page Element Positioning

Webdriver provides the following two ways to locate page elements, parameters are by pair, most commonly by.id and by.name lookups.

    • Findelement locates an element and throws an exception if no element is found:nosuchelementexception
    • Findelements locating a set of elements

For example, you need to locate the following elements:

<input class= "Input_class" type= "text" name= "passwd" id= "Passwd-id"/>

    • By.id:

webelement element = Driver.findelement (By.id ("Passwd-id"));

    • By.name:

webelement element = Driver.findelement (By.name ("passwd"));

    • By.xpath:

webelement element =driver.findelement (By.xpath ("//input[@id = ' Passwd-id ']");

    • By.classname

webelement element = Driver.findelement (By.classname ("Input_class"));

    • By.cssselector

webelement element = Driver.findelement (By.cssselector (". Input_class"));

    • By.linktext:

      The popular point is the exact query

Webdriver Driver = new Firefoxdriver ();
Driver.get ("http://www.baidu.com/");
webelement element = Driver.findelement (By.linktext ("Encyclopedia"));

    • By.partiallinktext:

This method is a fuzzy query
Webdriver Driver = new Firefoxdriver ();
Driver.get ("http://www.baidu.com/");
webelement element = Driver.findelement (By.partiallinktext ("Hao"));

    • By.tagname:

Webdriver Driver = new Firefoxdriver ();
Driver.get ("http://www.baidu.com/");
String test= driver.findelement (by.tagname ("form")). getattribute ("name");
SYSTEM.OUT.PRINTLN (test);

1.5 How to manipulate page elements 1.5.1 input box (text field or textarea)

webelement element = Driver.findelement (By.id ("Passwd-id"));

    • Element.sendkeys ("test");// Enter the contents in the Input box:
    • Element.clear (); // Empty the input box   
    • Element.gettext (); Get the text content of the input box:
1.5.2 drop-down selection box (select)

Select select = New Select (Driver.findelement (By.id ("select"));

    • Select.selectbyvisibletext ("A");
    • Select.selectbyvalue ("1");
    • Select.deselectall ();
    • Select.deselectbyvalue ("1");
    • Select.deselectbyvisibletext ("A");
    • Select.getallselectedoptions ();
    • Select.getfirstselectedoption ();
1.5.3 single option (Radio Button)

Webelement radio=driver.findelement (by.id ("Bookmode"));

    • Radio.click (); Select a single option
    • Radio.clear (); Clear a single option
    • Radio.isselected (); Determine if a single option has been selected
1.5.4 Multiple options (checkbox)

webelement checkbox = Driver.findelement (By.id ("MyCheckBox."));

    • Checkbox.click ();
    • Checkbox.clear ();
    • Checkbox.isselected ();
    • Checkbox.isenabled ();
1.5.5 buttons (button)

Webelement btn= driver.findelement (by.id ("Save"));

    • Btn.click (); Click the button
    • Btn.isenabled (); Determines whether the button is enable
1.5.7 Popup dialog box (popup dialogs)

Alert alert = Driver.switchto (). alert ();

    • Alert.accept (); Are you sure
    • Alert.dismiss (); Cancel
    • Alert.gettext (); Get text
1.5.8 form (form)

The actions of elements in a form, like other element operations, can be submitted to the form after the element operation is complete:

webelement approve = driver.findelement (By.id ("approve"));

Approve.click ();

Or

Approve.submit ();//only suitable for form submission

1.5.9 Uploading Files

To upload an element of a file operation:

Webelement adfileupload =driver.findelement (by.id ("Wap-upload"));

String FilePath = "C:\test\\uploadfile\\media_ads\\test.jpg";

Adfileupload.sendkeys (FilePath);

1.6 Switching between Windows and frames
    • Driver.switchto (). Defaultcontent (); Return to the top-most frame/iframe
    • Driver.switchto (). FRAME ("Leftframe"); Switch to a frame:
    • Driver.switchto (). Window ("Windowname"); Switch to a window
1.7 Calling Java Script

Web driver calls to Java script are implemented through Javascriptexecutor, for example:

Javascriptexecutor js = (javascriptexecutor) driver;

Js.executescript ("JS script");

1.8 Timeout settings

Webdriver Driver = new Firefoxdriver ();

      • Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Time-out when element is recognized
      • Driver.manage (). Timeouts (). Pageloadtimeout (Timeunit.seconds); Time-out at page load
      • Driver.manage (). Timeouts (). Setscripttimeout (Timeunit.seconds); Time-out for asynchronous scripts

selenium2.0 (Webdriver) API

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.