Interview #96: Selenium - What steps to take if an element is not clickable?
Software Testing Studio | WhatsApp 91-9606623245
Looking for Job change? WhatsApp 91-9606623245
When automating web applications using Selenium WebDriver, you may encounter a situation where an element is present in the DOM but not clickable. Clicking an element might fail due to various reasons such as overlays, dynamic page loading, or improper element identification. Here are the steps to diagnose and resolve this issue:
Disclaimer: For QA-Testing Jobs, WhatsApp us @ 91-9606623245
1. Understanding the Issue
An element might not be clickable due to:
2. Steps to Resolve the Issue
Step 1: Use Explicit Wait to Ensure Clickability
The element might not be ready to be clicked immediately after page load. Using explicit waits ensures the element is in a clickable state before performing an action.
Solution: Wait Until Element is Clickable
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("clickableElement")));
element.click();
Why?
Step 2: Scroll to the Element Before Clicking
Sometimes an element may not be visible within the viewport, preventing it from being clicked.
Solution: Use JavaScript Executor to Scroll
WebElement element = driver.findElement(By.id("clickableElement"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
element.click();
Why?
Step 3: Click Using JavaScript Executor
If Selenium’s click() method does not work due to overlays or event handlers, JavaScript can be used to perform the click.
Solution: JavaScript Click
WebElement element = driver.findElement(By.id("clickableElement"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
Why?
Step 4: Use Actions Class for Clicking
In some cases, the element is not recognized as clickable because of improper focus or styling issues.
Solution: Use the Actions Class
WebElement element = driver.findElement(By.id("clickableElement"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
Why?
Step 5: Check for Overlapping Elements
An overlay, popup, or other elements may be covering the target element.
Solution: Find and Remove the Overlay
领英推荐
WebElement overlay = driver.findElement(By.className("overlay-class"));
((JavascriptExecutor) driver).executeScript("arguments[0].style.display='none';", overlay);
OR Click elsewhere on the page before clicking the element:
Actions actions = new Actions(driver);
actions.moveByOffset(10, 10).click().perform(); // Clicks on an empty area
Why?
Step 6: Verify Locator Strategy
Sometimes, an incorrect locator might be identifying an element that is not interactive.
Solution: Verify Locator
Use debugging tools like Chrome DevTools (Inspect Element) to ensure you are selecting the right element.
WebElement element = driver.findElement(By.xpath("https://button[text()='Submit']"));
Why?
Step 7: Increase Click Wait Time
Some elements might take longer to be ready for interaction.
Solution: Add Small Delay Before Clicking
Thread.sleep(2000); // Not recommended for frequent use, but can be useful for debugging
element.click();
Why?
3. Best Practices to Avoid Clickability Issues
4. Conclusion
If an element is not clickable in Selenium WebDriver, the issue can arise due to visibility, overlays, loading delays, or incorrect selectors. The best approach is to:
By following these steps, you can effectively troubleshoot and resolve clickability issues in Selenium WebDriver. ??
QA lead
3 周Also need to check if element is located on frame or window in that case need to switch