</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

How do you handle dynamic elements without using XPath in Selenium?

Use CSS selectors with class name, ID prefix, or attribute matchers to locate dynamic elements without fragile XPath expressions.

Answer

Handling Dynamic Elements Without XPath

Dynamic elements have attributes (IDs, class names) that change with each page load. Instead of brittle XPath, use CSS selectors or other stable locators.

By class name:

Java
// If the element has a stable class even with a dynamic ID
driver.findElement(By.className("btn-primary"));

// Multiple classes — use CSS selector
driver.findElement(By.cssSelector(".login-form .submit-btn"));

By CSS selector (most flexible):

Java
// Attribute contains a value
driver.findElement(By.cssSelector("input[name='username']"));
driver.findElement(By.cssSelector("button[type='submit']"));

// Class starts with a stable prefix
driver.findElement(By.cssSelector("[class^='btn-']"));

// Partial attribute match
driver.findElement(By.cssSelector("[id*='login']"));    // ID contains "login"
driver.findElement(By.cssSelector("[id^='user_']"));    // ID starts with "user_"
driver.findElement(By.cssSelector("[id$='_btn']"));     // ID ends with "_btn"

By name attribute:

Java
driver.findElement(By.name("email"));
driver.findElement(By.name("password"));

By tag + attribute combination:

Java
// Find input with specific placeholder
driver.findElement(By.cssSelector("input[placeholder='Enter your email']"));

// Find link with data attribute
driver.findElement(By.cssSelector("a[data-action='login']"));

CSS selector pattern matching (vs XPath):

PatternCSSXPath equivalent
Attribute contains[id*='part']contains(@id, 'part')
Attribute starts with[id^='pre']starts-with(@id, 'pre')
Attribute ends with[id$='suf']No direct equivalent
Child elementdiv > input//div/input
Descendantdiv input//div//input

By link text (for anchor elements):

Java
driver.findElement(By.linkText("Login"));
driver.findElement(By.partialLinkText("Sign"));

Using @FindBy with stable CSS selectors in POM:

Java
@FindBy(css = "button[data-testid='submit']")
WebElement submitButton;

@FindBy(css = ".nav-menu li:first-child a")
WebElement firstNavItem;

Key answer: Handle dynamic elements by using class name or CSS selectors — they are faster than XPath and more resilient to DOM restructuring.

Follow AutomateQA

Related Topics