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();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s