How To Write Effective Xpath Selenium: Xpath //tag - Name (@attribute - Name 'Value')
How To Write Effective Xpath Selenium: Xpath //tag - Name (@attribute - Name 'Value')
How To Write Effective Xpath Selenium: Xpath //tag - Name (@attribute - Name 'Value')
What is Locator?
The locator can be termed as an address that identifies a web element uniquely
within the webpage. Locators are the HTML properties of a web element which
tells the Selenium about the web element it needs to perform the action on.
An XPath can be defined as a query language used for navigating through the
XML documents in order to locate different elements.
XPath defines the path of the element present on a web page. Following is the
standard syntax for creating XPath.
Xpath=//Tag_name[@attribute_name=’value’]
A single slash at the start of Xpath instructs XPath engine to look for element
starting from root node.
A double slash at the start of Xpath instructs XPath engine to search look for
matching element anywhere in the XML document.
We can find the location of any element on a web page using XML path
expressions. The basic syntax for XPath is shown below:
Syntax = //tagname[@attribute=’Value‘]
Example = //input[@id=’user-message‘]
XPath Description
S No. Locators
Absolute XPath
Starts with single slash “/” that means starting to search from the root.
Example: /html/body/div[2]/div/div[2]/div[1]/div[2]/form/div/input
Relative XPath
Starts from the middle of the HTML DOM.
Starts with a double slash “//” that means it can start to search anywhere in
the DOM structure.
Shorter than Absolute XPath.
Less fragile.
Example: //div[@class=’form-group’]//input[@id=’user-message’]
1) Basic Xpath: This kind of Xpath expression selects nodes or list of nodes on the
basis of attributes such as name, class name, id, etc. Following are the examples of
basic xpath expressions.
Xpath=//input [@type=’password’]
Xpath=//label [@id=’label1′]
Xpath=//input [@value=’RUN’]
Xpath=//*[@class=’test-tuts’]
Xpath=//a [@href=’http: //softwaretestingclass.com/’]
3) Using OR & AND: In the case of the OR expression, we use two conditions.
Here either 1st condition OR 2nd condition should be true. It is applied towhen one
condition is true or both. It means that at least one condition should be true to find
a web element. Following is the examples of OR expression.
Xpath=//*[@type=’submit’ OR @name=’buttonSubmit’]
In the case of the AND expression, we use two conditions. Here both conditions
should be true to locate a web element. It will fail to locate an element if any of the
conditions are false. Following are the examples of AND expression.
Xpath=//label[starts-with(@id, ‘message’)]
5) Text(): This expression is used with the text function to locate an element with
exact text. Following are the examples of text expression.
Xpath=//td
6) XPath axes methods: We use XPath axes methods are used to locate the
complex or dynamic web elements on the web page. Following are the Xpath axes
methods.
a)Following: This method selects all the elements in the HTML document from
the current node. Below is an example.
Xpath=//*[@type=’password’]//following::input
Xpath=//*[@type=’password’]//following::input[2]
b) Ancestor: This method selects all the ancestors’ element such as grandparent,
parent, etc. from the current node. Below is an example.
Xpath=//*//ancestor::p
Xpath=//*//ancestor::div [2]
c) Child: This method selects all the children elements from the current node.
Below is an example.
Xpath=//*[@id=’navigation-list’]/child::li
Xpath=//*[@id=’navigation-list’]/child::li[2]
d) Preceding: This method selects all the nodes that come before the current node.
Below is an example.
Xpath=//*[@type=’text’]//preceding::input
Xpath=//*[@type=’text’]//preceding::input[3]
e) Following-sibling: This method Select the following siblings from the context
node. Siblings are located at the same level of the current. Below is an example.
xpath=//*[@type=’text’]//following-sibling::input
f) Parent: This method selects the parent of the current node. Below is an
example.
Xpath=//*[@id=’soft-test-class’]//parent::div
Xpath=//*[@id=’soft-test-class’]//parent::div[1]
Xpath =//*[@type=’text’]//self::input
Xpath=//*[@id=’soft-test-class’]//descendant::a
Xpath=//*[@id=’soft-test-class’]//descendant::a[1]
Key Points:
The success rate of finding an element using Xpath is too high. Along with
the previous statement, Xpath can find relatively all the elements on a web
page. Thus, Xpaths can be used to locate elements having no id, class or
name.
Creating a valid Xpath is a tricky and complex process. There are plug-ins
available to generate Xpath but most of the times, the generated Xpaths fails
to identify the web element correctly.
While creating xpath, the user should be aware of the various nomenclatures
and protocols.