N NextGen Virtual Academy
AD Administrator Learning Manager
SELENIUM WEBDRIVER · PRACTICE LAB

Dynamic XPath

Master dynamic XPath techniques for elements whose attributes, text or positions change during execution.

SELENIUM XPath
OBJECTIVE

What You Will Practice

  • Identify dynamic attributes.
  • Use XPath contains().
  • Use XPath starts-with().
  • Locate elements by text().
  • Use normalize-space().
  • Combine conditions using and / or.
  • Navigate parent and child elements.
  • Practice XPath axes.
01

contains()

Locate an element when only part of an attribute value is stable.

CONTAINS
XPath //button[contains(@id,'dynamicLoginButton')]
02

starts-with()

Locate an element when its attribute begins with a known value.

STARTS-WITH
XPath //input[starts-with(@id,'userField_')]
03

text()

Locate an element using its visible text.

TEXT
XPath //button[text()='Edit Employee']
04

normalize-space()

Handle unwanted spaces around element text.

TEXT
XPath //button[normalize-space()='Submit Form']
05

and / or Conditions

Combine multiple XPath conditions to create more reliable locators.

CONDITIONS
XPath //input[contains(@id,'employeeEmail') and @type='email'] //input[@type='email' or @type='password']
06

Parent & Child

Navigate from a parent element to its child element.

RELATIONSHIP
Rahul Kumar Automation Engineer
XPath //div[@id='employeeCard']//button
07

XPath Axes

Practice following-sibling, preceding-sibling and ancestor.

AXES
Employee ID EMP101
Employee Name Arjun Kumar
Department QA Automation
XPath //span[text()='Employee Name']/following-sibling::strong //strong[@id='axisEmployeeName']/ancestor::div[@class='axis-row']
08

Dynamic Employee Table

Use dynamic XPath to locate an action belonging to a specific employee.

REAL-TIME
Employee Role Action
Priya Sharma QA Engineer
Arjun Kumar Automation Engineer
Meena Rao Test Lead
Dynamic XPath //div[contains(@class,'dynamic-table-row')][.//span[text()='Arjun Kumar']]//button
QUICK REFERENCE

Dynamic XPath Cheat Sheet

Contains
//button[
    contains(
        @id,
        'login'
    )
]
Starts With
//input[
    starts-with(
        @id,
        'user_'
    )
]
Text
//button[
    text()='Login'
]
Normalize Space
//button[
    normalize-space()
    ='Submit'
]
Parent / Child
//div[@id='card']
    //button
Following Sibling
//span[
    text()='Name'
]
/following-sibling::strong