Application of XPath in selenium Webdriver

Source: Internet
Author: User
Tags tagname xpath xpath in selenium guru99

In selenium automation, if the ID, class, name, etc General Locator cannot find the element, the XPath is used to find the element on the Web page.

This time I will explain from the following three aspects:

1. What is XPath

2. Use of XPath syntax

3. Use XPath to handle complex and dynamic elements in selenium

1. XPath is an abbreviation for the XML Path language and is primarily used to select nodes in the document in the XML document. Based on the XML tree-like document structure, XPath can be used to find the specified node in the whole tree.

XPath positioning has great flexibility in that a node in the XML document tree can be searched forward or backward. CSS positioning can only be searched forward in the XML document tree, but XPath is slower to locate than CSS.

is the basic form of XPath

Syntax for XPath:

xpath=//tagname[@attribute = ' value ']
: Selects the current node, which is the location of all levels in the document.
Tagname: node Name
@: Select Properties.
Attribute: The attribute name of the node.
Value: The values of the property.
2. XPath syntax
Here we mainly talk about the relative path, absolute path is basically not used, know on the line.
For relative XPath, the path begins in the middle of the HTML DOM structure. It starts with a double forward slash (//), which means it can search for elements anywhere on the page.

Common formats are as follows: positioning as elements

Relative XPath://*[@class = ' Featured-box ']//*[text () = ' testing ']

3, what is the XPath axis.

The XPath axis searches for different nodes in the XML document from the current context node. An XPath axis is a method used to find dynamic elements, otherwise it cannot be implemented by ordinary XPath methods, such as no id,classname,name.

The Axis method is used to find elements that change dynamically during a refresh or any other operation. There are few axes used in Selenium webdriver, such as son, Father, ancestor, brother, predecessor, ego, etc.

Using XPath to handle complex and dynamic elements in selenium
1) Basic XPath:

The XPath expression selects the node or node list based on attributes such as id,name,classname in the XML document, as shown below.

xpath=//input[@name = ' uid ']

Here are more basic common expressions:

xpath=//input[@type = ' text ']xpath=//label[@id = ' Message23 ']xpath=//input[@value = ' RESET ']xpath=//*[@class = ' Barone ']xpath=//a[@href = ' http://demo.guru99.com/')
xpath=//img[@src = '//cdn.guru99.com/images/home/java.png ')

2) Contains (): Contains () is the method used in the XPath expression. Use it when the value of any property changes dynamically, such as login information.

The include feature can find elements with partial text, as shown in the following example.

In this example, we try to identify the element using only a partial literal value of the property. In the following XPath expression, use the partial value ' sub ' instead of the submit button. The element can be observed to be successfully found.

The full value of "type" is "submit", but only the partial value "sub" is used.

XPath =//* [Contains (@type, ' Sub ')]
The full value of ' name ' is ' Btnlogin ', but only partial value ' BTN ' is used.

Xpath=.//*[contains (@name, ' btn ')]

In the above expression, we use ' name ' as the property, and ' btn ' as part of the value, as shown in the following screen. This will find 2 elements (Login&reset), because their ' name ' attribute begins with ' btn '.

In the following expression, we use the linked "text" as a property and "here" as a partial value, as shown in the following screen.
This will find the link (' here ') because it shows the text ' here
Xpath=//*[contains (Text (), ' here ')]xpath=//*[contains (@href, ' guru99.com ')]

3) Use Or&and:

In an or expression, use two conditions, whether the first condition or the second one should be true. If any one condition is true or both, it also applies. means that any one condition should be true to find the element.

In the following XPath expression, it identifies an individual or two elements that have a condition of true.

xpath=//*[@type = ' Submit ' OR @name = ' btnreset ')

In an and expression, two conditions are used, and two conditions should be true to find the element. If any one of the conditions is false, the element cannot be found. xpath=//input[@type = ' Submit ' and @name = ' btnlogin ')

4) start-with function: start-with function Find the element or any action on the Web page when the property value changes when the refresh occurs. In this expression, the starting text of the matching attribute is used to find the element whose property is dynamically changed. You can also find elements with property values that are static (not changed).

For example, suppose that the ID of a particular element changes dynamically, such as:

Id = "Message12"

Id = "message345"

Id = "message8769"

Wait a minute.. But the original words were the same. In this case, we use the start-with expression.
Xpath=//label[starts-with (@id, ' message ')]

5) Text (): In this expression, using the text function, we find the element with the exact text match, as shown below. In our example, we find elements with the text "UserID".
Xpath=//td[text () = ' UserID ']

6) XPath axis method: These XPath axis methods are used to find complex or dynamic elements. We'll look at some of these methods below.

A)following: Select all elements after the current node () [The UserID input box is the current node], as shown in the following screen.

xpath=//*[@type = ' text ']//following::input

By using the "following" axis password, login and reset buttons, there are 3 "input" nodes to match. If you want to focus on any particular element, you can use the following XPath methods:

xpath=//*[@type = ' text ']//following::input[1]

You can do this by typing [1],[2] .... Wait to change the XPath as needed.

When the input is "1", the following screen finds the specific node "password" input box element

b)Ancestor:Ancestor Axis Select all ancestor elements of the current node (grandparents, parents, etc.), as shown in the following screen.

That is, all the upper nodes of the current node

In the following expression, we find the ancestor element of the current node (the "ENTERPRISE Testing" node).

Xpath=//*[text () = ' Enterprise testing ']//ancestor::d IV

Match 13 "div" nodes by using the "Ancestor" axis. If you want to focus on any particular element, then you can use the following XPath to change the number 1, 2, according to your requirements;
Xpath=//*[text () = ' Enterprise testing ']//ancestor::d iv[1]

c) Child: Selects all children of the current node (Java), as shown in the following screen.

xpath=//*[@id = ' java_technologies ']/child::li

Match 71 "Li" nodes by using the "child" axis. If you want to focus on any particular element, you can use the following XPath:
xpath=//*[@id = ' java_technologies ']/child::li[1]

d) preceding: Select all nodes before the current node, as shown in the following screen.

In the following expression, it identifies all input elements before the "LOGIN" button, the userid and password input elements.

xpath=//*[@type = ' Submit ']//preceding::input

Match 2 input nodes by using the preceding axis. If you want to focus on any particular element, then you can use the following XPath:

xpath=//*[@type = ' Submit ']//preceding::input[1]

e) follow-sibling: Select the following sibling nodes of the context node. Siblings are at the same level as the current node,

That is, the peer node of the current node

As shown in the following screen. It will find the element after the current node.

xpath=//*[@type = ' Submit ']//following-sibling::input

f) Parent: Select the parent node of the current node, as shown in the following screen.

xpath=//*[@id = ' rt-feature ']//parent::d IV

Use the "parent" axis to match the 65 "div" nodes. If you want to focus on any particular element, then you can use the following XPath:

xpath=//*[@id = ' rt-feature ']//parent::d iv[1]

g) Self: Select the current node or "I" to indicate that it indicates the node itself, as shown in the following screen.

Xpath =//*[@type = ' password ']//self::input

h) descendant: Selects all downlevel nodes of the current node

xpath=//*[@id = ' rt-feature ']//descendant::a

xpath=//*[@id = ' rt-feature ']//descendant::a[1]

XPath axes is the method used to find dynamic elements, otherwise it cannot be found through the normal XPath method.

Application of XPath in selenium Webdriver

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.