Selenium Locating page elements

Source: Internet
Author: User
Tags tag name tagname xpath

First Position Element Accessibility tool

IE, right click on the element, "check element", or press F12 key to open the developer tool;

In Chrome, right-click the "Review element" on the element, or press F12 to open the developer tool;

Firefox right-click on elements, "check elements", or install plugins firebug open developer tools;

http://getfirebug.com/

Xpathchecker can be installed in Firefox to locate elements on the page through XPath.

https://addons.mozilla.org/zh-CN/firefox/addon/xpath-checker/

Just put the firebug arrow on the element you want to position, you can get the value of the element corresponding to the tag, similar in other browsers.

Second Find method

Take the wing payment portal as an example to locate this input box by using properties such as name, ID, class, LinkText, Partiallinktext, CSS selector, and so on.

<input name= "Productno" id= "Phoneno" type= "text" maxlength= "one" class= "ui-txtinput01" autocomplete= "Off" value= " Please enter your phone number "/>

/**
* <li class= "Login-btn01" >
<a class= "Ui-button ui-button-morange Ui-loginbutton" id= "Telloginbuttn" href= "javascript:void (0);" > &nbsp;&nbsp; Records </a>
</li>
*/

1. Search by ID is the most effective and convenient method
Webelement Phonenobyid = driver.findelement (By.id ("Phoneno"));

Webelement Loginbyid = driver.findelement (By.id ("TELLOGINBUTTN"));

2. Search by name, method similar to ID
Webelement phonenobyname = driver.findelement (By.name ("Productno"));

3. Using class name lookup, a good way to clean sweep some elements with the same class, if the entire page has only one element using the class can also be used.
Webelement phonenobyclassname = driver.findelement (By.classname ("ui-txtinput01"));

4. the link text and the partial link text lookup, used in the positioning of hyperlinks more.

Webelement Loginbylinkedtext = driver.findelement (By.linktext ("login"));
Webelement Loginbypartiallinktext = driver.findelement (By.partiallinktext ("Deng"));

5. Through the CSS selector find is also commonly used to find methods, various browser support is very good, look fast.
Webelement loginbycssselector = driver.findelement (By.cssselector ("A.ui-button.ui-button-morange.ui-loginbutton") );

Locate the div element with ID flrs, which can be written as: #flrs, note://div[equivalent to the XPath syntax @id = ' Flrs ')
Locate the A element under FLRS, which can be written as #flrs > A, note://div[equivalent to the XPath syntax @id = ' Flrs ']/a
Locate an element with an HREF attribute value of/forexample/about.html under ID flrs, which can be written as: #flrs > a[href= "/forexample/about.html"]
If you need to specify multiple property values, you can add them one after the other, such as #flrs > input[name= "username"][type= "text"]

6. Through XPath lookup is a last resort method, in the above methods can not be found when the use of XPath lookup, almost to locate any element on the page, but the positioning performance is not very good, so still try to use less. You can use the Xpathcecker plugin installed in Firefox to check that the XPath is correct. Absolute paths are represented by a single/number, while relative paths are represented by//. When the path of the XPath starts with/begins, the XPath parsing engine is parsed from the root node of the document. When the XPath path starts with//, it means that the XPath engine parses from any conforming element node of the document. When/appears in the XPath path, it means looking for the parent node's immediate child node, when//appears in the XPath path, to look for any child node under the parent node that matches any criteria, regardless of how many levels have been nested.

Webelement phoneNoByXpath1 = driver.findelement (By.xpath ("//input[@id = ' Phoneno ']");
Webelement phoneNoByXpath2 = driver.findelement (By.xpath ("//input[@name = ' Productno ']");
Webelement phoneNoByXpath3 = driver.findelement (By.xpath ("//input[@class = ' ui-txtinput01 ']");
Webelement loginByXpath1 = driver.findelement (By.xpath ("//a[contains (@href, ' JavaScript ')]");
Webelement loginByXpath2 = driver.findelement (By.xpath ("//a[starts-with (@class, ' Ui-button ')]");

7. When you want to locate the same element for a group of elements, you can find it by tagname in addition to the name lookup. This method finds elements through the tag name of the element, usually with more than one element searched, so it is generally used in conjunction with the Findelements method.

list<webelement> buttons = driver.findelements (By.tagname ("button"));
list<webelement> inputs = Driver.findelements (By.tagname ("input"));
list<webelement> alinks = driver.findelements (By.tagname ("a"));

8. Some special elements, such as the wing payment on the first page of the mouse hover on the "Life service" when the "utility coal payment" will show, find difficulties, you can directly execute JS code in the driver.

<a class= "header-tab-a" href= "/appcenter/lifepay/water" > Hydro-coal Payment </a>
Javascriptexecutor js = (javascriptexecutor) driver;
String myjs= "Document.getelementsbyclassname (' header-tab-a ') [0].click ()";
Js.executescript (MYJS);

9. Some pages use an IFRAME control to access another Web page, and the elements in the IFRAME control cannot be found directly, and must be switch to the IFRAME before they can be found.

Webelement elementinframe = Driver.switchto (). FRAME ("Frameid"). Findelement (By.xpath ("//a[@id = ' signIn ']/input"));

If a page has a frame inside it, you need to switch to the first frame and switch to the second frame.

Find ifrome1 First (id = F1)
Driver.switchto (). FRAME ("F1");
Then find the ifrome2 below it (ID =f2)
Driver.switchto (). FRAME ("F2");
The following elements can be manipulated as normal.
Driver.findelement (By.id ("kw")). SendKeys ("Selenium");
Driver.findelement (By.id ("su")). Click ();

If a page has two sibling frames, switch to the first frame to find the elements in the second frame, you need to switch to defaultcontent, then switch to the second frame.

First find the ifrome1 (id = f1)
Driver.switchto (). FRAME ("F1");
Driver.findelement (By.id ("kw")). SendKeys ("Selenium");
Driver.findelement (By.id ("su")). Click ();
Then find the ifrome2 below it (ID =f2)
Driver.switchto (). Defaultcontent ();
Driver.switchto (). FRAME ("F2");
The following elements can be manipulated as normal.
Driver.findelement (By.id ("kw")). SendKeys ("Selenium");
Driver.findelement (By.id ("su")). Click ();

Webdriver often finds a cannot find elements error when the element has not yet been loaded. At this time can be 3 kinds of waiting to let webdriver wait a moment.

The explicit wait is to wait until the code uses an explicit wait element to satisfy the specified expectedcondition, and so on, and so on, unless it is not found within the specified timeout period, then jump out of exception.

Webelement mydynamicelement = (new webdriverwait (Driver, ten)). Until (Expectedconditions.presenceofelementlocated ( By.xpath ("")));

There are a lot of waiting conditions for display waiting can be set, specifically see the Expectedconditions class method:

The implicit wait is set for Webdriver, and when Webdriver finds any element, it will wait for the time set here if it cannot be found.

Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);

Thread wait is to let the program wait for a fixed time, not recommended for use.

Thread.Sleep (WaitTime)

Selenium Locating page elements

Related Article

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.