Java & Selenium Web Driver. The reason At point (x, y), the element is not clickable. An additional element would be clicked.

Introduction to Selenium WebDriver

Selenium WebDriver is an open-source automation structure, meant for the testing of web-based applications. It finds wide adoption within industries in a combination cross-operating system, something unique about it. It enables developers and quality assurance professionals to create test programs which, in turn, launch browsers, input values to forms, click buttons, etc, programmatically. Eventually, with Selenium Webdriver, execution of tests that need a browser, a Chromium or Firefox browser, is possible, but no GUI is required.

In Java, integration of Selenium WebDriver with testing frameworks like TestNG and JUnit is coherent. So, writing automated test scripts is easier, along with the execution and expected results verification.

How does WebDriver interact with WebElements in Java?

Basically, WebDriver interacts with elements on a web page through the set of commands that get executed in the browser. Each element, be it buttons, text fields, or links, has to be located first using locators like id, name, className, CSSSelector, XPath, etc. In simple words, WebDriver finds an element on a page using one of these locators and then invokes actions like click(), sendKeys(), or getText() on it.

The normal interaction with a web element in Java would include:

WebDriver driver = new ChromeDriver();
driver.get("https://example.com");

// Locate an element by ID
WebElement button = driver.findElement(By.id("submit"));

// Perform a click action on the button
button.click();

In order to mimic user behaviors, WebDriver carries out these operations at the Document Object Model (DOM) level, interacting with the underlying HTML structure.

Clickability of Elements is Important for Selenium Tests

Clickability is one of the main concerns in a Selenium test. Doing an action, such as form submission or navigation, or testing of a feature normally requires it to simulate an end-user click on buttons, links, or any interactive element. If, due to page load delays, overlapping, or dynamic content, an element is not clickable, then tests fail, hence giving unreliable results.

Clickability in automation is not just about finding the element-it needs to be visible, enabled, and not masked by other elements. Such test scenarios demand correct handling for reliable automation. Debugging click issues will involve understanding those factors that affect the visibility, access, and readiness of elements to be interacted with.

The Clickability Problem in Selenium

Common Reasons for Elements Not Being Clickable

One of the most prevalent problems when using the Selenium WebDriver involves attempting to click an element that is not clickable. Elements might appear clickable but aren’t for various reasons, which are essential to comprehend in order to resolve it.

1.Element is Hidden or Overlapped

  • Description: An element is considered covered when another element is overlaid on top of it. A good example of this could be a banner, a modal, or a loading spinner. WebDriver will know the position of the element but it won’t be in a position to act upon it since something else is physically covering it in the viewport.
  • Example: there is a pop-up advertisement or cookie consent banner on top, but even though the button behind it is found, it cannot be clicked.
WebElement button = driver.findElement(By.id("submit"));
button.click(); // May fail due to another element on top of i

2.Element Not Completely Loaded

  • Description: Sometimes the web page or, to be more precise, that particular element itself is not completely loaded and the WebDriver tries to interact with the element. The WebDriver may try to click the element before it is fully ready, thus fails.
  • Example: A button or link that appears after JavaScript has rendered dynamic content is not clickable until rendering is complete.

3.Incorrect Element Positioning or CSS Styling

  • Description: Sometimes, with respect to loading of the page, an element may have its position shifted or it may be styled in such a way that only a portion of it is clickable. This could be due to z-index or some layout issues, or transformations in CSS.
  • Example: A button having low z-index may fall behind the elements with higher z-index values. Thus, one would not be in a position to click on that button.

4. Dynamic Changes on the Page

  • Description: Web pages can contain dynamic content that is able to load extra elements on the fly, for example via AJAX; this will make clickability fail. In case the DOM changes between the moment the element was located and the moment the action is performed, the element is either not present or not in the state.
  • Example: A button’s position or visibility changes right after WebDriver locates it but before it clicks, leading to the click action failing.

5. Element is Outside the Viewport

  • Description: Elements which are not in view (for example, because they are to the right of an area which is wider than the viewport) may not be manipulable even though they exist according to WebDriver. In order to interact with such elements, one must first scroll the area into view such that the element is within the viewport.
  • Example: A submit button at the end of a long form may need to be scrolled into view prior to cli

6. Element inside an Iframe

  • Description: Elements inside iframes are a little sensitive, meaning that WebDriver has to switch to the frame prior to acting on elements inside the iframe.
  • Example: A “Click Me” button inslide of an iframe will be not clickable unless and until the WebDriver switches to the appropriate frame.

Why It’s Possible to Click on an Extra Element Rather Than the One You Meant to

  1. Overlapping Elements
  2. Timing Issues
  3. CSS and Positioning Problems
  4. Element is Offscreen or Not in Focus

In conclusion, WebDriver is a position-based system, meaning that clicking on unintentional items might occur due to a variety of circumstances such as overlapping elements, delayed loading times, or improper CSS style. To guarantee that the right element is interacted with in Selenium test automation, these problems need to be handled properly.

Reasons for Clickability Problems:

Overlapping Elements:

The most common cause of clickability issues that arise in Selenium WebDriver is an overlapping element over the target element, be it a banner, a pop-up, or any type of loading animation. Even if the WebDriver correctly identifies the intended element through something like a locator, such as By.id or By.xpath, it may not be able to click on it if another element physically overlaps the target in the viewport. In that case, it would click on that overlapping element other than the one it was told to interact with, hence causing failures in tests.

For instance, a occluding pop-up advertisement or a cookie consent banner might block the view of a target “Submit” button from Selenium’s perspective so it cannot click the button.

Techniques to Handle Overlapping Elements

1.Use Explicit Waits to Ensure Page Load Completion
Often, overlapping issues arise because the page or certain elements haven’t fully loaded. In such cases, the best approach is to use an Explicit Wait to wait until the target element is visible and interactable. Selenium provides the WebDriverWait class to handle such situations.

Example:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));
button.

  • This method ensures that Selenium waits until the element is clickable before attempting the action.

2.Clicking Elements Directly with JavaScriptExecutor If everything above does not work for you and some element is overlaid by another, then you can use JavaScriptExecutor to bypass normal WebDriver behaviors and directly invoke the target element click() event. While this works, it’s generally considered a workaround, and may not simulate a real user’s interaction with a page.

Example:

WebElement button = driver.findElement(By.id("submit"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", button);

  • This forces a click on the element regardless of visibility, ensuring that the correct element is clicked.

3.Dismiss or Handle Pop-ups and Overlays If a pop-up or banner is covering the element, explicitly handle or dismiss it. Selenium allows you to interact with pop-ups (modals, cookie banners, alerts) and close them before interacting with the intended element.

Example:

WebElement popup = driver.findElement(By.id("popup-close"));
if (popup.isDisplayed()) {
    popup.click();  // Close the popup before interacting with other elements
}

WebElement button = driver.findElement(By.id("submit"));
button.click();

  • In this case, the script checks if a pop-up is visible and closes it, ensuring the underlying element is clickable.

4.Check if Element is Actually Clickable It is helpful to programmatically verify if the element is overlapped by other elements using JavaScript to check whether an element is currently obstructed by another element at the same coordinates.

Example:

WebElement button = driver.findElement(By.id("submit"));
Boolean isDisplayed = (Boolean) ((JavascriptExecutor) driver).executeScript(
    "var elem = arguments[0], box = elem.getBoundingClientRect(), " +
    "cx = box.left + box.width / 2, cy = box.top + box.height / 2, " +
    "e = document.elementFromPoint(cx, cy); " +
    "return e === elem;", button);

if (isDisplayed) {
    button.click();  // Click only if the element is not covered
} else {
    System.out.println("Element is overlapped by another element.");
}

  • This code checks if the center of the target element is the top-most element at that point in the viewport, ensuring that it’s not being covered by another element.

5.Scroll the Element into View In some cases, an element might not be visible due to its position off-screen or within a scrollable div. By using JavaScriptExecutor to scroll the element into view, you can ensure it becomes visible and clickable.

Example:

WebElement button = driver.findElement(By.id("submit"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", button);
button.click();  // Click after scrolling the element into view

6.Use Action Class for Precision When the element is interactable but slightly obstructed or misaligned, you can use Selenium’s Actions class to hover or perform a click more precisely.

Example:

Actions actions = new Actions(driver);
WebElement button = driver.findElement(By.id("submit"));
actions.moveToElement(button).click().perform();

Page Load and Timing Issues

Sometimes, elements may not be fully loaded or rendered by the time WebDriver tries to interact with them, leading to click failures. This happens particularly when the webpage uses dynamic content (e.g., AJAX requests or JavaScript rendering), where elements load asynchronously.

Techniques to Handle Page Load and Timing Issues:

1.Use Explicit Waits for Element Visibility: Wait until the element is visible or clickable before performing any action.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicElement")));

2.Use Implicit Waits: Implicit waits provide a global timeout for all WebDriver commands, though they are less precise compared to explicit waits.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicElement")));

3.Handle Loading Spinners: Ensure that loading spinners or progress indicators have disappeared before interacting with the element.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loadingSpinner")));

Element Position Changes

Dynamic content can make elements on the page shift and result in situations where Selenium attempts to click an element that moves just before the click happens. This happens very often when the pages have real-time updates or infinite scroll features.

Techniques to Handle Element Position Changes:

1.Use ExpectedConditions.stalenessOf(): This ensures that if an element is replaced in the DOM or changes state, Selenium will wait until the new element is stable before interacting.

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.stalenessOf(oldElement));

2.Scroll to the Element: Ensure the element is within view before clicking by scrolling to its position.

JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement button = driver.findElement(By.id("submit"));
js.executeScript("arguments[0].scrollIntoView(true);", button);
button.click();

3.Double-check the Element’s Coordinates Before Clicking: If an element’s position changes after locating it, the click might happen in the wrong location. Use dynamic checks to verify the coordinates before interacting.

Point location = button.getLocation();
if (driver.manage().window().getSize().getHeight() > location.getY()) {
    button.click();
}

Z-index and CSS Styling Issues

The z-index attribute of CSS controls the order in which elements are stacked on a web page. In case two elements overlap and one is above another due to a higher value of its z-index, the WebDriver would try to click it instead of the one beneath it, and the intended element would not get clicked.

Techniques to Handle Z-index and CSS Styling Issues:

1.Check Element Z-index: You can use JavaScript to inspect an element’s z-index and ensure no other elements with higher z-index values are obstructing it.

JavascriptExecutor js = (JavascriptExecutor) driver;
Long zIndex = (Long) js.executeScript("return window.getComputedStyle(arguments[0]).zIndex;", button);

2.Use JavaScriptExecutor to Bring the Element to the Front: If a lower-z-index element is blocked, you can temporarily adjust its z-index to bring it to the front.

js.executeScript("arguments[0].style.zIndex='9999';", button);

3.Use Actions Class for Precision Clicking: The Actions class allows for more precise interactions, which can be useful when dealing with elements that are affected by CSS positioning.

Actions actions = new Actions(driver);
actions.moveToElement(button).click().perform();

Advanced Techniques for Handling Clickability Issues

Taking Screenshots for Debugging

For that, capturing screenshots during a Selenium test helps identify which elements are covering your target click. Screenshots provide you with visual inspection of the state of the page at that moment, when a test fails, if there’s a banner or a pop-up covering the supposed clickable element.

How to Capture a Screenshot in Selenium: Selenium WebDriver provides the TakesScreenshot interface to capture screenshots.

Example:

File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File destinationFile = new File("screenshot.png");
FileUtils.copyFile(screenshot, destinationFile);

  • This will save the screenshot as an image file, which can be opened and inspected to see what’s preventing the click.

Screenshots for Debugging:

  1. Identify Obstruction: From the screenshot, check if something was overlapping the button or link to be clicked.
  2. Check Page Load State: Use the screenshot to verify whether the page has fully loaded or if certain sections are still rendering.
  3. Check Element Visibility: Ensure the desired element is in view and not out of sight or invisible.

Switching to Frames or Windows

Most modern web applications make use of iframes or open their elements in new browser windows. If your target element resides inside an iframe or a new window, until the context is switched to the correct frame or window, Selenium WebDriver will not be able to interact with it.

Switching to an iframe: In case your element is inside an iframe, before acting on the element, driver.switchTo().frame() needs to switch to the iframe.

Example:

// Switch to iframe by index, name, or WebElement
driver.switchTo().frame("iframeName");
WebElement button = driver.findElement(By.id("submit"));
button.click();

// Switch back to the default content after interaction
driver.switchTo().defaultContent();

Switching to a New Window: Sometimes, a click might open a new browser window or tab. Selenium can switch control to the newly opened window using driver.switchTo().window().

Example:

// Store the current window handle
String mainWindow = driver.getWindowHandle();

// Perform some action that opens a new window
driver.findElement(By.id("newWindowButton")).click();

// Switch to the new window
for (String windowHandle : driver.getWindowHandles()) {
    driver.switchTo().window(windowHandle);
}

// Interact with elements in the new window
WebElement newButton = driver.findElement(By.id("submit"));
newButton.click();

// Switch back to the original window
driver.switchTo().window(mainWindow);

Handling Alerts or Modals

Alerts and modals are common causes of clickability issues, as they often block interaction with other elements on the page. Selenium WebDriver provides dedicated methods to handle browser alerts and JavaScript-based modal dialogs.

Managing JavaScript Alerts: Until they are rejected or approved, JavaScript alerts have the ability to prevent clicks on components.

Example:

// Wait for alert and switch to it
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
Alert alert = wait.until(ExpectedConditions.alertIsPresent());

// Accept the alert
alert.accept();  // Or use alert.dismiss() to dismiss

Handling Modal Dialogs: Modal dialogs or pop-ups might require interaction before proceeding with the rest of the test. Typically, these dialogs contain buttons for actions like “OK” or “Cancel,” which need to be clicked before interacting with other elements on the page.

Example:

// Wait for modal to appear
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement modal = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("modal-dialog")));

// Interact with the modal, such as clicking a close button
WebElement closeButton = modal.findElement(By.className("close"));
closeButton.click();

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.