How To Write Dynamic Xpath in Selenium:: Findelement (By - Xpath ("Xpath") )

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

How To Write Dynamic XPath In Selenium:

Before learning how to write dynamic XPath in Selenium, we will


learn what is XPath locator.

XPath Locator:

XPath is designed to allow the navigation of XML documents, with


the purpose of selecting individual elements, attributes, or some
other part of an XML document for specific processing. XPath
produces reliable locators but in performance wise it is slower
(especially in IE older versions) compared to CSS Selector.

Syntax:

findElement(By.xpath("XPath"));
Sometimes, we may not identify the element using the locators
such as id, class, name, etc. In those cases, we use XPath to find an
element on the web page. Check this link to identify the xpath using
firepath plugin. At times, XPath may change dynamically and we
need to handle the elements while writing scripts. Standard way of
writing xpath may not work and we need to write dynamic XPath in
selenium scripts. Let’s see different way of writing dynamic XPath in
Selenium with examples:
1. Using Single Slash
2. Using Double Slash
3. Using Single Attribute
4. Using Multiple Attribute
5. Using AND
6. Using OR
7. Using contains()
8. Using starts_with()
9. Using text()
10. Using last()
11. Using position()
12. Using index()
13. Using following xpath axes
14. Using preceding xpath axes

Here I am trying to find the element (Email field) on Gmail Login


Page
HTML Code: (Gmail login page – Email field)

<input id="Email" type="email" <span class="html-attribute-


name">value</span>="" <span class="html-attribute-
name">spellcheck</span>="<span class="html-attribute-
value">false</span>" class="emailClass"
autofocus="" <span class="html-attribute-
name">name</span>="<span class="html-attribute-
value">Email</span>" placeholder="Enter your email"/>

1. Using Single Slash:

This mechanism is also known as finding elements using Absolute


XPath.
Single slash is used to create XPath with absolute path i.e. the
XPath would be created to start selection from the document
node/start node/parent node.

Syntax:

html/body/div[1]/div[2]/div[2]/div[1]/form/div[1]/div/div[1]/div/div/in
put[1]
2. Double Slash:

This mechanism is also known as finding elements using Relative


XPath.

Double slash is used to create XPath with relative path i.e. the
XPath would be created to start selection from anywhere within the
document. – Search in a whole page (DOM) for the preceding string

Syntax:

//form/div[1]/div/div[1]/div/div/input[1]
3. Single Attribute:

You could write the syntax in two ways as mentioned below.


Including or excluding HTML Tag. If you want to exclude HTML Tag
then you need to use *

Syntax:

//<HTML tag>[@attribute_name='attribute_value']
or
//*[@attribute_name='attribute_value']
Note: ‘*‘ after double slash is to match any tag with the desired text
XPath based on above HTML:

//input[@id='Email']
or
//*[@id='Email']
4. Multiple Attribute:

Syntax:

//<HTML tag>[@attribute_name1='attribute_value1'][@attribute_name2='attribute_value2]
or
//*[@attribute_name1='attribute_value1'][@attribute_name2='attribute_value2]

5. Using AND:

Syntax:

//<HTML tag>[@attribute_name1='attribute_value1' and


@attribute_name2='attribute_value2]
or
//*[@attribute_name1='attribute_value1' and @attribute_name2='attribute_value2]

XPath based on above HTML:


//input[@id='Email' and @name='Email']
or
//*[@id='Email' and @name='Email']

6. Using OR:

Syntax:

//<HTML tag>[@attribute_name1='attribute_value1' or @attribute_name2='attribute_value2]


or
//*[@attribute_name1='attribute_value1' or @attribute_name2='attribute_value2]
XPath based on above HTML:
//input[@id='Email' or @name='Email']
or
//*[@id='Email' or @name='Email']

7. contains(): It is used to identify an element, when we are


familiar with some part of the attributes value of an element.

Syntax:

//<HTML tag>[contains(@attribute_name,'attribute_value')]
or
//*[contains(@attribute_name,'attribute_value')]

XPath based on above HTML:


//input[contains(@id,'Email')]
or
//*[contains(@id,'Email')]
or
//input[contains(@name,'Email')]
or
//*input[contains(@name,'Email')]

8. starts-with(): It is used to identify an element, when we are


familiar with the attributes value (starting with the specified text) of
an element.

Syntax:

//<HTML tag>[starts-with(@attribute_name,'attribute_value')]
or
//*[starts-with(@attribute_name,'attribute_value')]
XPath based on above HTML:
//input[starts-with(@id,'Ema')]
or
//*[starts-with(@id,'Ema')]

9. text(): This mechanism is used to locate an element based on


the text available on a webpage

As per the above image, we could identify the elements text based
on the below xpath.

//*[text()='New look for sign-in coming soon']


10. last(): Selects the last element (of mentioned type) out of all
input element present
To identify the element (last text field ) “Your current email
address”, we could use the below xpath.

findElement(By.xpath("(//input[@type='text'])[last()]"))
To identify the element “Year”, we could use the below xpath.

[last()-1] – Selects the last but one element (of mentioned type)
out of all input element present

findElement(By.xpath("(//input[@type='text'])[last()-1]"))
11. position(): Selects the element out of all input element
present depending on the position number provided
In below given xpath, [@type=’text’] will locate text field and
function [position()=2] will locate text filed which is located on
2nd position from the top.

findElement(By.xpath("(//input[@type='text'])[position()=2]"))
or
findElement(By.xpath("(//input[@type='text'])[2]"))
12. Finding elements using index

By providing the index position in the square brackets, we could


move to the nth element. Based on the below xpath, we could
identify the Last Name field.

findElement(By.xpath("//label[2]"))
13. following: By using this we could select everything on the web
page after the closing tag of the current node

xpath of the FirstName field is as follows

//*[@id='FirstName']
To identify the input field of type text after the FirstName field, we
need to use the below xpath.
//*[@id='FirstName']/following::input[@type='text']
Here I used, following xpath axes and two colons and then specified
the required tag (i.e., input)

To identify just the input field after the FirstName field, we need to
use the below xpath.

//*[@id='FirstName']/following::input
14. preceding: Selects all nodes that appear before the current
node in the document, except ancestors, attribute nodes and
namespace nodes
xpath of the LastName field is as follows

//*[@id='LastName']
To identify the input field of type text before the LastName field, we
need to use the below xpath.
//*[@id='LastName']//preceding::input[@type='text']"
Here I used, preceding xpath axes and two colons and then
specified the required tag (i.e., input).

If you have any queries, please comment below in the comment


section. Like this post? Don’t forget to share it!

Here are few hand-picked articles for you to read next:

You might also like