SELENIUM BASICS:

  • What is Automation Testing - Advantages & Types of Tools
  • What is Selenium IDE, Selenium RC, Selenium WebDriver, Selenium Grid?
  • Selenium IDE, Firebug & Firepath Installation

  • SELENIUM WEBDRIVER:

  • Selenium WebDriver Architecture
  • Selenium Installation
  • Challenges and Limitations of Selenium
  • First Selenium WebDriver Script
  • Working with Firefox Browser
  • Solution - Failed To Launch Browser Using Selenium WebDriver
  • Working with Chrome Browser
  • Working with IE Browser
  • Difference Between FindElement & FindElements
  • Install Firebug, Fire path and other add on for Selenium
  • Locators in Selenium
  • Dynamic Xpath
  • Dynamic CSS
  • Handle drop-downs
  • How to work with file upload
  • Handle Alerts & Popups
  • Handle Multiple Windows
  • Mouse Hover event in Selenium
  • Right Click
  • Double Click
  • Drag and Drop
  • Capture Screenshots in Selenium
  • Parameterization
  • Synchronization
  • Implicit Wait, Explicit Wait and Fluent Wait in Selenium
  • Implicit Waits
  • Explicit WebDriver Waits
  • Fluent Wait In Selenium
  • Apache POI
  • How To Read Data From Excel File
  • How To Write Data In Excel File
  • Database Testing Using MSSQL
  • Database Testing Using MySQL
  • Database Testing Using DB2
  • Handle Ajax Call in Selenium
  • Listeners in Selenium
  • Handling JavaScript in Selenium
  • Stale Element Reference Exception in Selenium
  • Integrate Sikuli with Selenium
  • Headless Browser Testing 
  • Headless Browser Testing using HtmlUnitDriver
  • Headless Browser Testing using PhanthomJSDriver

  • TESTNG:

  • TestNG Installation
  • TestNG Complete Tutorial

  • REPORTS:

  • Extent Reports Introduction
  • Extent Reports Generation
  • Capturing Screenshots & Including In Extent Reports
  • Extent Reports Version 4

  • BUILD & CONTINUOUS INTEGRATION TOOLS:

  • ANT Build Tool - How To Download & Install
  • Maven Build Tool - How To Download & Install
  • Creating Maven Selenium Project
  • GIT Integration with Selenium
  • Jenkins - How To Download & Install
  • Jenkins - Executing TestNG Scripts
  • Jenkins - Generating Reports After Test Execution
  • Jenkins - Executing Maven Project
  • Create build and Execute build for nightly execution
  • Send Email report based on build Status
  • Selenium Continuous Integration with Jenkins [Selenium – Maven – Git – Jenkins] – Step By Step Guide

  • CROSS BROWSER TESTING:

  • Running Selenium Tests On BrowserStack
  • Kobiton - Mobile Testing Platform with Real Devices

  • MOBILE TESTING:

  • Mobile Testing Platform with Real Devices

  • AUTOMATION FRAMEWORKS:

  • What is Framework & Types of Framework
  • Page Object Model Design Pattern
  • Data Driven Framework
  • How to explain Test Automation Framework or Selenium Automation Framework Architecture In Interview

  • API Testing:

  • Learn API Testing
  • Postman Tutorial (API Testing with Postman)

  • INTERVIEW QUESTIONS:

  • Test Automation Framework Interview Questions
  • Selenium Interview Questions
  • TestNG Interview Questions
  • Java Interview Questions
  • Python Interview Questions
  • API Testing Interview Questions

  • SAMPLE RESUME:

  • Automation Tester Resume
  • How To Write Dynamic XPath In Selenium WebDriver

    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

    Get our latest blog posts delivered to your inbox

    Subscribe and get popular blog posts about software testing industry.

    Rajkumar

    14 thoughts on “How To Write Dynamic XPath In Selenium WebDriver”

    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