Working with Dynamic IDs in Selenium Java
Web automation with Selenium becomes challenging when dealing with dynamic element IDs—those that change every time a page is refreshed or reloaded. In such cases, traditional selectors like By.id("some-id") fail because the exact value of the ID is unpredictable. To build robust and reliable test automation scripts, testers must learn how to identify and interact with such elements using alternative and smart locator strategies.
In this blog, we’ll explore how to handle dynamic IDs in Selenium Java, including common patterns, effective strategies, and best practices.
🔍 Why Dynamic IDs Are a Problem
Dynamic IDs are usually generated by frontend frameworks or JavaScript dynamically when the page loads. For example:
html
<input id="user_162834" type="text" name="username">
Every time the page reloads, the number suffix changes (user_837465, user_483920, etc.). If you write your Selenium code like this:
java
Copy
driver.findElement(By.id("user_162834")).sendKeys("testuser");
…it will fail the next time the ID changes.
✅ Solution 1: Use Partial Match with XPath contains()
Instead of matching the full ID, use the contains() method in XPath to match the stable portion of the ID.
java
Copy
Edit
driver.findElement(By.xpath("//input[contains(@id, 'user_')]")).sendKeys("testuser");
This finds any <input> tag with an ID that contains the string user_.
✅ Solution 2: Use CSS Selectors with Wildcards
CSS Selectors also support partial matches, such as:
^= (starts with)
$= (ends with)
*= (contains)
Example:
java
Copy
Edit
driver.findElement(By.cssSelector("input[id*='user_']")).sendKeys("testuser");
This will match any input element whose id contains the substring user_.
✅ Solution 3: Use Other Stable Attributes
Sometimes elements have other stable attributes like name, placeholder, class, aria-label, or even data-* attributes that don’t change.
java
Copy
Edit
driver.findElement(By.name("username")).sendKeys("testuser");
Or:
java
Copy
Edit
driver.findElement(By.xpath("//input[@placeholder='Enter your username']")).sendKeys("testuser");
Always inspect the element for these static properties before relying on ID or class names.
✅ Solution 4: Traverse the DOM Hierarchy
If no attributes are stable, consider navigating from a nearby stable element using relative XPath.
Example:
html
Copy
Edit
<label for="user_1283">Username</label>
<input id="user_1283" type="text">
Selenium code:
java
Copy
Edit
driver.findElement(By.xpath("//label[text()='Username']/following-sibling::input")).sendKeys("testuser");
This selects the input field that directly follows the label with text "Username".
✅ Solution 5: Leverage Custom Attributes (data-*)
Many modern applications include data-* attributes specifically for test automation:
html
Copy
Edit
<input data-test-id="username-input" type="text">
Selenium:
java
Copy
Edit
driver.findElement(By.cssSelector("input[data-test-id='username-input']")).sendKeys("testuser");
These are ideal selectors because they are stable and designed not to interfere with the UI logic.
🧪 Best Practices
Avoid using dynamic id or class attributes directly in your locators.
Prefer XPath or CSS selectors with stable patterns or sibling/ancestor relationships.
Collaborate with developers to add custom attributes for testing (data-test, aria-*, etc.).
Always validate your locator in different sessions to ensure it remains consistent.
🔚 Conclusion
Dynamic IDs are a common obstacle in Selenium automation, but with smart strategies like using XPath functions, CSS wildcard selectors, and stable attribute targeting, you can write resilient and reusable test scripts in Java. Mastering these locator techniques is essential for any automation engineer working on dynamic web applications.
Learn Selenium with Java Training
Read More: Testing Responsive Web Design with Selenium Java
Read More: Handling Calendar and Date Pickers in Selenium Java
Visit Our IHUB Talent Institute Hyderabad
Get Direction
Comments
Post a Comment