Handling JavaScript Pop-ups in Selenium Python

One of the most common challenges testers face when automating web applications is dealing with dynamic web elements. These are elements whose attributes such as ID, name, class, or XPath change frequently, especially after every page reload or session. Selenium WebDriver, a widely used automation tool for web testing, offers several strategies to effectively interact with such elements, even in dynamic environments. In this blog, we’ll explore how to handle dynamic web elements in Selenium using Java.


๐Ÿ”„ What Are Dynamic Web Elements?

Dynamic elements are web components whose properties change dynamically. For example:

  • Changing IDs on each reload (id="user_123", user_456, etc.)
  • Elements loaded asynchronously (AJAX or JavaScript rendering)
  • Appearing/disappearing popups or menus
  • Data-driven UI (like dropdowns or tables populated from APIs)

These changes make it difficult to identify and interact with elements using static locators.


๐Ÿ› ️ Strategies for Handling Dynamic Elements in Selenium

1. Use Dynamic XPath with contains() or starts-with()

Instead of using a fixed ID or class, use XPath functions that match patterns.

java


// ID that partially matches

driver.findElement(By.xpath("//*[contains(@id,'user_')]")).click();


// Class name starts with a pattern

driver.findElement(By.xpath("//*[starts-with(@class,'btn-')]")).click();

These flexible locators adapt to minor changes in attributes.


2. Wait for Element Visibility Using WebDriverWait

Dynamic elements might not be immediately present in the DOM. Use explicit waits to wait until the element is visible or clickable.


java


WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebElement dynamicElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicId")));

dynamicElement.click();

This ensures your script doesn't fail due to timing issues.


3. Use CSS Selectors When IDs or XPaths Fail

CSS selectors can often locate elements more efficiently than XPath.

java

driver.findElement(By.cssSelector("button[class*='submit']")).click();

This locates a button element with a class that includes “submit,” even if the full class name is dynamic.


4. Leverage Relative Locators (Selenium 4 Feature)

Selenium 4 introduces relative locators which allow locating elements in relation to others.

java

WebElement label = driver.findElement(By.xpath("//label[text()='Username']"));

WebElement input = driver.findElement(RelativeLocator.with(By.tagName("input")).below(label));

input.sendKeys("testUser");

This is useful when IDs or classes are dynamic but the layout is consistent.


5. JavaScript Executor for Hidden/Dynamic Elements

For tricky elements that aren’t easily interactable using WebDriver, use JavaScript.

java

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("arguments[0].click();", element);

This bypasses normal WebDriver mechanisms and performs a direct DOM interaction.


๐Ÿงช Example: Handling a Search Box with Dynamic ID

Suppose a search box has a dynamic ID like search_12345. You can locate and interact with it as follows:


java

WebElement searchBox = driver.findElement(By.xpath("//input[contains(@id,'search_')]"));

searchBox.sendKeys("Selenium WebDriver");

searchBox.sendKeys(Keys.ENTER);

This approach avoids brittle selectors and ensures your script runs reliably.


✅ Best Practices

  • Avoid using hardcoded wait times (Thread.sleep()); always use WebDriverWait.
  • Regularly inspect the HTML structure for stable patterns like text labels or placeholder attributes.
  • Log interactions and failures to debug dynamic behavior.
  • Modularize your locator strategy in utility methods for reuse and maintainability.


๐Ÿ“ Conclusion

Handling dynamic web elements is a crucial part of building robust and reliable Selenium test scripts. By using dynamic XPath functions, waits, CSS selectors, and relative locators, you can interact with elements that change over time or vary between sessions. The key is to adapt your locator strategy based on the behavior of the element, ensuring your tests remain stable even as the application evolves.

Learn Selenium with Pyhton Training Hyderabad
Read More: Using Assertions in Selenium Python Tests


Visit IHUB Talent Institute Hyderabad
Get Direction

Comments

Popular posts from this blog

How to Use Tosca's Test Configuration Parameters

Installing Java and Eclipse IDE for Selenium Automation

How Flutter Works Behind the Scenes