How To Write Dynamic XPath In Selenium WebDriver | Software Testing Material

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:

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

Learn How To Write Dynamic CSS Selector In Selenium WebDriver [Without Any Tool]

Here is a video tutorial to learn “Creating dynamic XPath in Selenium WebDriver”:

Please be patient. The video will load in some time.

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

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

Gmail Email Field

HTML Code: (Gmail login page – Email field)

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:

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:

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: 

Note: ‘*‘ after double slash is to match any tag with the desired text

XPath based on above HTML:

4. Multiple Attribute:

Syntax:

XPath based on above HTML:

5. Using AND:

Syntax:

XPath based on above HTML:

6. Using OR:

Syntax:

XPath based on above HTML:

7. contains(): It is used to identify an element, when we are familiar with some part of the attributes value of an element.

Syntax:

XPath based on above HTML:

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:

XPath based on above HTML:

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

Gmail New Look Text

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

10. last(): Selects the last element (of mentioned type) out of all input element present

Gmail Registration Page

To identify the element (last text field ) “Your current email address”, we could use the below xpath.

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

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.

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.

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

To identify the input field of type text after the FirstName field, we need to use the below xpath.

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.

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

To identify the input field of type text before the LastName field, we need to use the below xpath.

 

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:

Selenium Tutorial
TestNG Tutorial
Selenium Interview Questions
TestNG Interview Questions
Manual Testing Tutorial
Manual Testing Interview Questions

Write Dynamic Xpath In Selenium

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

14 thoughts on “How To Write Dynamic XPath In Selenium WebDriver | Software Testing Material”

  1. Superb!!! Thanks for sharing this great information.

    • Glad you liked it Sreenivas. Keep visiting.

  2. Great Post!!!!!! You saved my times , do include some demos of xpath axes

    • Thanks Rahul. I am glad you found it helpful. Yeah, we have a plan to write an article on xpath axes. Subscribe our blog for latest updates.

  3. Thanks Raj Kumar
    Excellent video and info is good

    • Thanks for your kind words Prasad

  4. Thanks Raj,
    It helps me alot.
    Great job.

    • I am glad it helped. Keep visiting our site Santhosh.

  5. helpful post. saved my time
    thank you Rajkumar.

  6. In 5th) Using AND:

    @attribute_name2=’attribute_value2] ===> @attribute_name2=’attribute_value2′]

    Ending single quote (‘) is missing.

    • Thanks will update it

  7. Very nice explaination about xpath.
    Its really very much helpfull for us.

  8. thanks a lot .

    Will it will be helpful for finding xpath for any dynamic text or link with the help of following and preceding xpath which are not dynamic in nature ?

    • Yes

Leave a Comment

Share via
Copy link
Powered by Social Snap