2

I have to write a test to verify if an element is NOT clickable under certain circumstances. I'm using the Selenium Webdriver Java library.

I know how to do the opposite thing by utilizing the WebDriverWait and ExpectedConditions classes.

 new WebDriverWait(element, TIMEOUT).until(ExpectedConditions
 .elementToBeClickable(By.cssSelector(LOCATOR)));

but I can't seem to find a way to check the opposite thing.

I've gone through the docs for WebElement and managed to break down the problem into two simpler cases.

  1. The element is disabled - in this case I can just use WebElement#isEnabled and wrap it in a predicate passed to WebDriverWait#until.

    new WebDriverWait(driver, TIMEOUT).until(new Predicate<WebDriver>(){
     public boolean apply(WebDriver driver) {
     return !getElement().isEnabled();
     }
    }); 
    
  2. In some cases, the element does not do anything on click. I want to take this into account as well. The problem is, the element is not disabled per se. It can be clicked but there is no event registered (usually, there is one but in my case, it's being removed by JavaScript).

The second case still troubles me. Is there any way I can wait for such an event to be unregistered?

BoltClock
728k165 gold badges1.4k silver badges1.4k bronze badges
asked Dec 20, 2013 at 9:08

1 Answer 1

3

Your solution for case 1 seems like it should work.

For case 2, you'll probably need to invoke JavaScript on the page from within Selenium to check if the element has any event handlers assigned, I believe the following should do the trick:

new WebDriverWait(driver, TIMEOUT).until(new Predicate<WebDriver>() {
 public boolean apply(WebDriver driver) {
 return (JavascriptExectutor) driver.executeScript(
 "arguments[0].onclick == null;", getElement());
 }
}); 

This should get JS to query the current onClick event handler assigned to the element passed into the JavascriptExecutor from the method getElement() and return true if no handler is assigned.

Not actually tested this beyond a quick fiddle with the JS, but it should be all good.

Update: In newer selenium versions, you should be able to use:

ExpectedConditions.not(ExpectedConditions.elementToBeClickab‌​le(By))
answered Dec 22, 2013 at 2:55

5 Comments

Thanks, I'll try this when I get back to work after the X-mas break. The explicit cast looks a bit suspicious but it can probably be gotten rid of, I'll see what I can do about this. Thanks again, happy holiday ;)
I'm pretty sure that's just how the JS executor on Selenium works, may well be wrong however - if you do find a way to get rid of it I'd be happy to hear it! Did you have any luck with this answer by the way?
I ended up using the JavaScriptExecutor to check for a specific value set by the front end. Thanks.
Apparently you can just do ExpectedConditions.not(ExpectedConditions.elementToBeClickable(By))
Pretty sure that wasn't supported at the time this was written. Will update the answer to include this.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.