Use By.xpath to quickly locate page elements in Selenium webdriver __selenium

Source: Internet
Author: User
Tags xpath


Take the login page password box as an example to explain how to locate the page element in the selenium webdriver by By.xpath, quickly get the element position and complete the operation.

Problem Introduction:

The following script is recorded with the Selenium IDE:

Driver.findelement (By.name ("Pass")). Clear ();

Driver.findelement (By.name ("Pass")). SendKeys ("password");

Driver.findelement (by.id ("Passwords")). Clear ();

Driver.findelement (by.id ("Passwords")). SendKeys ("123456");

Playback, the page is very difficult to locate, it takes a long time to find the Password box and enter the password, if the timeout is not found will be an error.

Solution:

Since there are times when you can replay success, and sometimes not find the page elements, it must be the element positioning is not accurate, so in the search will consume a long time, then through the Xpah positioning can be.

XPath finds an element object in such a way that it is positioned through//, for details, refer to: http://www.w3school.com.cn/xpath/xpath_syntax.asp

//

Select the nodes in the document from the current node that matches the selection, regardless of their location.

For example, the Password box is input box inputs, to find the location of input can be achieved through//input, if only//input may be positioned to multiple input elements, this time need to pass the key value for more accurate positioning, syntax is//input[@key = Value], i.e.://input[@name = ' Pass ']

Driver.findelement (By.xpath ("//input[@name = ' pass ']"). Clear ();

Driver.findelement (By.xpath ("//input[@name = ' pass ']")). SendKeys ("password");

Driver.findelement (By.xpath ("//input[@id = Passwords]")). Clear ();

Driver.findelement (By.xpath ("//input[@id = Passwords]")). SendKeys ("123456");

Replay, the time is still very long, it seems that through an element key value to the positioning is not reliable, that can be through multiple elements. Just like the advanced descriptive programming in QTP. What is the syntax of XPath, how many elements key values are connected to each other?

First look for another element of this element of the Name=pass key value pair: class= Textfild, use these two positioning for a try.

Driver.findelement (By.xpath ("//input[@name = ' pass ' and @class = ' textfild ']"). Clear ();

Driver.findelement (By.xpath ("//input[@name = ' pass ' and @class = ' textfild ')"). SendKeys ("password");

Driver.findelement (By.xpath ("//input[@id = passwords and @class = ' textfild required ']"). Clear ();

Driver.findelement (By.xpath ("//input[@id =passwords and @class = ' textfild required ')"). Sendkes ("12.");

A return visit, the speed is really fast, quickly recognized the position of the password box, and entered.

The XPath lookup element can also be implemented by contains (a string lookup function), which is

Input[contains (@id, Vakue) and contains (@id, value)], in which the ID and value are the key value pairs that locate the INPUT element

For example:

Input[contains (@class, ' Textfild ') andcontains (@name, ' Pass ')]

The final integration and simplification of the 4-sentence code can be simplified to the following two sentences

Driver.findelement (By.xpath ("//input[contains (@class, ' Textfild ') and contains (@name, ' Pass ')]"). Clear ();

Driver.findelement (By.xpath ("//input[@id = ' passwords ' and @class = ' textfild required ' and @type = ' password ']"). SendKeys ("123456");

Why can be simplified to 2 lines, look at the screenshot above, in fact, when the password box has a "password" two words, when the mouse moved into the password box to gain focus, "password" text disappeared, users can enter their own real password. So

Driver.findelement (By.xpath ("//input[@name = ' pass ' and @class = ' textfild ')"). SendKeys ("password");

This sentence is something we don't need. Because when the password box gets the focus, the "password" text disappears, so the following empty is not necessary.

Driver.findelement (By.xpath ("//input[@id = passwords and @class = ' textfild required ']"). Clear ();

In this way, the code is simplified.

Playback, everything OK.

 

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.