04_01_Selenium WebDriver Locators - Part6-XPath in Selenium WebDriver
04_01_Selenium WebDriver Locators - Part6-XPath in Selenium WebDriver
Selenium WebDriver
Locators – Part 6
May-2019
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
1
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.
XPath in Selenium WebDriver
What is XPath?
XPath is defined as XML path. It is a syntax or language for
finding any element on the web page using XML path
expression. XPath is used to find the location of any element on
a webpage using HTML DOM structure. The basic format of XPath
is explained below with screen shot.
XPath in Selenium WebDriver
Syntax for XPath:
XPath contains the path of the element situated at the web page.
Standard syntax for creating XPath is.
Xpath=//*[@type='submit' or @name='btnReset']
Using OR & AND:
Using OR & AND:
In AND expression, two conditions are used, both conditions
should be true to find the element. It fails to find element if any
one condition is false.
Xpath=//input[@type='submit' and
@name='btnLogin']
Using OR & AND:
Start-with function:
Start-with function finds the element whose attribute value
changes on refresh or any operation on the webpage. In this expression,
match the starting text of the attribute is used to find the element whose
attribute changes dynamically. You can also find the element whose
attribute value is static (not changes).
For example -: Suppose the ID of particular element changes dynamically
like:
Id=" message12"
Id=" message345"
Id=" message8769"
and so on.. but the initial text is same. In this case, we use Start-with
expression.
Start-with function:
Xpath=//label[starts-
with(@id,'message')]
Text():
XPath axes methods:
These XPath axes methods are used to find the complex or dynamic elements. Below we will see some of these methods.
- Following
- Ancestor
- Child
- Preceding
Following :
Selects all elements in the document of the current node( ) [ UserID input box is the current node] as shown in the below screen.
Following :
Xpath=//*[@type='text']//following::input
Following :
Xpath=//*[@type='text']//following::input[1]
Ancestor :
The ancestor axis selects all ancestors element (grandparent, parent, etc.) of
the current node as shown in the below screen.
Xpath=//*[text()='Enterprise Testing']//ancestor::div
Ancestor :
There are 13 "div" nodes matching by using "ancestor" axis. If you want to focus on any particular element then you can use the below XPath, where
you change the number 1, 2 as per your requirement:
Xpath=//*[text()='Enterprise
Testing']//ancestor::div[1]
You can change the XPath according to the requirement by putting [1], [2]
…………and so on.
Child :
Selects all children elements of the current node (Java) as shown in the below screen.
Xpath=//*[@id='java_technologies']/child::li
Child :
There are 71 "li" nodes matching by using "child" axis. If you want to focus on any particular element then you can use the
below xpath:
Xpath=//*[@id='java_technologies']/child::li[1]
You can change the XPath according to the requirement by putting [1], [2]
…………and so on.
Preceding :
Select all nodes that come before the current node as shown in the below screen.
Xpath=//*[@type='submit']//preceding::input
Preceding :
There are 2 "input" nodes matching by using "preceding" axis. If you want to focus on any
particular element then you can use the below XPath:
Xpath=//*[@type='submit']//preceding::input[1]
You can change the XPath according to the requirement by putting [1], [2]
…………and so on.
Following-sibling :
Select the following siblings of the context node. Siblings are at the same level of the current node as shown in the below screen. It will
find the element after the current node.
xpath=//*[@type='submit']//following-sibling::input
Parent :
Selects the parent of the current node as shown in the below screen.
Xpath=//*[@id='rt-feature']//parent::div
Parent :
There are 65 "div" nodes matching by using "parent" axis. If you want to focus on any
particular element then you can use the below XPath:
Xpath=//*[@id='rt-feature']//parent::div[1]
You can change the XPath according to the requirement by putting [1], [2]
…………and so on.
Self :
Selects the current node or 'self' means it indicates the node itself as shown in the below screen.
Xpath =//*[@type='password']//self::input
Descendant :
Selects the descendants of the current node as shown in the below screen.In the below expression, it identifies all the element descendants
to current element ( 'Main body surround' frame element) which means down under the node (child node , grandchild node, etc.).
Xpath=//*[@id='rt-feature']//descendant::a
Descendant :
There are 12 "link" nodes matching by using "descendant" axis. If you want to focus on any
particular element then you can use the below XPath:
Xpath=//*[@id='rt-feature']//descendant::a[1]
You can change the XPath according to the requirement by putting [1], [2]
…………and so on.
Summary
XPath is required to find an element on the web page as to do an operation on
that particular element.
•There are two types of XPath:
• Absolute XPath
• Relative XPath
•XPath Axes are the methods used to find dynamic elements, which otherwise
not possible to find by normal XPath method
•XPath expression select nodes or list of nodes on the basis of attributes like ID ,
Name, Classname, etc. from the XML document .
Exercise
1.Go to http://demo.guru99.com/test/guru99home/
2.In the section 'A few of our most popular courses', search all Web Elements which are sibling
of a WebElement whose text is 'SELENIUM'
3.We will find element using contains , ancestor and sibling function..
Following - Preceding
Ancestor - Descendant
Parent - Child
Following-sibling
1. Write methods with parameters like this : selectMenuItem(String para1)
2. Write test cases such below :
1.Launch the web browser and open the site "Demo Guru99 Page “
2.Using method above to select any item via parameter input.
Example : selectMenuitem(Testing);
3.Check point for action at step2.
RBVH/ENG1
Reference
https://www.guru99.com/xpath-selenium.html
45
Internal | RBVH/ENG1 | 6/24/2014 | © Robert Bosch Engineering and Business Solutions Vietnam Company Limited 2013. All
rights reserved, also regarding any disposal, exploitation, reproduction, editing, distribution, as well as in the event of
applications for industrial property rights.