1

I have my code below:

IWebElement elem = null;
WebDriverWait wait = new WebDriverWait(DriverContext.Driver, TimeSpan.FromSeconds(20));               
elem = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[contains(string(@id),'confirm')]/div/div[2]/a[1]")));
elem.Click();

I tried to run and it would give me below exception:

System.InvalidOperationException: unknown error: Element is not clickable at point (437, 505). Other element would receive the click: <td style="width: 60%; padding-left: 5px; text-align: left;">...</td>
  (Session info: chrome=54.0.2840.71)
  (Driver info: chromedriver=2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf),platform=Windows NT 6.1.7601 SP1 x86_64)
Result StackTrace:  
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebElement.Click()

Now I tried to use the implicitly waits:

DriverContext.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));
elem = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[contains(string(@id),'confirm')]/div/div[2]/a[1]")));
elem.Click();

Same exception error is displayed. But then I used Thread.Sleep() instead of implicitly waits- which I know is a bad practice, it miraculously worked. Am I on the right path?

2
  • When you use ImplicitlyWait, does it fail on the DriverContext.Driver.FindElement line or on the wait.Until line? – user246 Nov 4 '16 at 18:56
  • @user246 sorry I updated the implicitly waits part, it fails on the wait.Until line – Marj Nov 4 '16 at 19:23
1

The name is misleading. The expectation ElementToBeClickable doesn't really check that the element is clickable. It checks that the element is displayed and not disabled.

On the other hand, the click also checks that the element on top at the coordinates of the click is your element or a descendant. This additional test ensures that the element is really visible from a user perspective. Unfortunately, there's no built-in condition to test it.

If in your case you are facing a transition effect, then try to wait for a minimum height/width:

WebElement elem = wait.Until(ExpectedConditions.presenceOfElementLocated(By.XPath(...)));
wait.until((WebDriver drv) -> elem.getSize().getHeight() > 15 ? element : null)
    .click();

Or try to click your element until it succeeds :

wait.until((WebDriver drv) -> {
    try {
        elem.click();
        return true;
    } catch (WebDriverException ex) {
        return false;
    }
});
1

This is a known problem with ExpectedConditions.ElementToBeClickable. See https://github.com/angular/protractor/issues/2313 for details.

As I understand it, if you use ImplicitlyWait, wait.Until will keep trying the specified predicate until the condition is true or the timeout expires. As the above bug report describes, ExpectedConditions.ElementToBeClickable ought return false if the specified element is hidden behind another element, but instead it throws an exception.

Thread.Sleep works because it waits long enough for your element to appear.

You might try calling ExpectedConditions.visibilityOf (inside waitUntil) first to ensure the element is visible before calling ExpectedConditions.ElementToBeClickable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.