Cannot click element? Execute JavaScript or use the Actions class

Have you ever seen this error in Chrome when trying to click on an element?

org.openqa.selenium.WebDriverException: 
Element is not clickable at point (411, 675)

Clicking the specific element works fine in Firefox but not at all in Chrome.

The code is very straightforward:

By locator = By.xpath("//a[attribute='value']");

WebElement element = driver.findElement(locator);

element.click();

So this does not work sometimes in Chrome.

What do you do?

 

Use an explicit wait

WebDriverWait wait = new WebDriverWait(driver, 25);

By locator = By.xpath("//a[attribute='value']");

WebElement element = wait.until(ExpectedConditions.
                     elementToBeClickable(locator));

element.click();

Unfortunately, this does not work either.

 

 Execute JavaScript

JavascriptExecutor jsExecutor;
jsExecutor = (JavascriptExecutor) driver;

String jsQuery;

jsQuery = "document.querySelector(\"[attribute='value']\").click()"; 

jsExecutor.executeScript(javaScriptCommand);

This code works sometimes only.

So, what do we do next?

 

Use the Actions class to chain methods

WebDriverWait wait = new WebDriverWait(driver, 25);

By locator = By.xpath("//a[attribute='value']");

WebElement element = wait.until(ExpectedConditions.
                                elementToBeClickable(locator));

new Actions(driver()).moveToElement(element)
                     .click()
                     .perform();
Advertisement

One Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s