Vous êtes sur la page 1sur 6

How to Write Effective XPath Selenium

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.

XPath Selenium Selectors

The syntax for XPath:

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’]

 // : It is used to select the current node.

 Tag_name: It is the name of the tag of a particular node.

 @: It is used to select to select attribute.

 Attribute_name: It is the name of the attribute of the node.

 Value: It is the value of the attribute.

Difference between single ‘/’ or double ‘//’

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

1. ID It is used to find an element by ID of that element

It is used to find an element by the Class name of that


2. Class name
element

3. Name It is used to find an element by the name of that element

4. Link text It is used to find an element by the text of that link

It is required for locating the dynamic element and to


5. XPath
traverse among various elements present on the web page

CSS path is used to locate web elements which have no


6. CSS path
name, class or ID.

Absolute and Relative XPath

Absolute XPath

 It is a direct way to locate an element.

 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’]

Handling complex & dynamic web elements by using XPath:

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/’]

2) Contains(): It is a method that is used in an XPath expression. When the value


of any attribute changes dynamically e.g. login information, this method come into
use. It can locate a web element with the available partial text. Following are the
examples of contains () method.

 Xpath=//*[contains (@type, ‘sub-type’)]


 Xpath=.//* [contains (@name, ‘button’)]
 Xpath=//*[contains(@id, ‘login’)]
 Xpath=//*[contains(text (),’testing’)]
 Xpath=//*[contains (@href,’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=//input[@type=’submit’ AND @name=’buttonEnter’]


4) Start-with function: This function is used to find a web element whose value
of attribute changes on the refresh or on any dynamic operation on the web
page. In this expression, we match the starting text of the attribute that is used to
locate an element whose attribute has changed dynamically. E.g.On the web page
ID of a particular element changes dynamically such as ‘id1’, ‘id2’, ‘id3’, etc. but
the text remains the same. Following are the examples of starts-with 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]

g) Self: This method selects the current node. Below is an example.

 Xpath =//*[@type=’text’]//self::input

h) Descendant: This method selects the descendants of the current. Below is an


example.

 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.

Vous aimerez peut-être aussi