Reputation: 1021
I am getting the following dreaded error when using Chromedriver with Selenium:
"org.openqa.selenium.WebDriverException: Element is not clickable at point (xxx, yyy). Other element would receive the click: ..."
I know this has been discussed here
However my situation is a bit different in the sense if I put a delay of about 5s and then click it works fine. I don't have to do anything special, just wait.
I know I can force a click by using JS or Actions, but I want a more elegant way of handling this i.e. only click button when button becomes clickable. The problem is I don't know how to check and see if the button is clickable or not.
I've tried the following but neither work:
1) ExpectedConditions.elementToBeClickable
2) ExpectedConditions.visibilityOf
Any ideas?
Upvotes: 0
Views: 1118
Reputation: 3384
I handled this using custom waitAndClick() method which uses recursion as following:
int waitCounter = 0;
// Wait for an element to become clickable
public static void WaitAndClick(WebElement elementToBeClicked) throws InterruptedException, IOException {
try
{
WebDriverWait wait = new WebDriverWait(driver, 20);
WebDriverWait wait1 = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOf(elementToBeClicked));
wait1.until(ExpectedConditions.elementToBeClickable(elementToBeClicked));
elementToBeClicked.click();
}
catch(Exception e)
{
MethodLibrary.Logger_Info("Element not clicked yet. waiting some more for " + elementToBeClicked);
if(waitCounter <3){
waitCounter++;
WaitAndClick(elementToBeClicked);
}
waitCounter = 0;
}
}
Upvotes: 0
Reputation: 6910
I can think of two options you could try:
JavaScript
Check the Document.readyState
property that describes the loading state of the document:
JavascriptExecutor jsExecutor = (JavascriptExecutor) input;
return jsExecutor.executeScript("return document.readyState;").equals("complete");
You can just wait for it to become "complete". Might not be work in many cases but worth a try.
Loop
That's probably the simplest solution. Not sure about its elegance but should do the job without wasting more time than needed (This is just a very basic example, this code should not be used as-is):
while (true) {
try {
element.click();
} catch (WebDriverException e) {
// ignore
Thread.sleep(millis);
}
Upvotes: 2