How To Write Dynamic Xpath in Selenium:: Findelement (By - Xpath ("Xpath") )
How To Write Dynamic Xpath in Selenium:: Findelement (By - Xpath ("Xpath") )
How To Write Dynamic Xpath in Selenium:: Findelement (By - Xpath ("Xpath") )
XPath Locator:
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
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:
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:
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:
6. Using OR:
Syntax:
Syntax:
//<HTML tag>[contains(@attribute_name,'attribute_value')]
or
//*[contains(@attribute_name,'attribute_value')]
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')]
As per the above image, we could identify the elements text based
on 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
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
//*[@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).