11

I have a container, which contains number of elements. I am looping thru the elements. My question is what is the syntax for checking if the element is not clickable.

1

7 Answers 7

15

Following should make it work -

element.is_displayed() and element.is_enabled()

This code is in Python. You can change it to the language of your choice.

Sign up to request clarification or add additional context in comments.

Comments

5

The existing methods, isDisplayed and isEnabled cannot check for whether the element is clickable or not.

If you want to wait for element till it is clickable and then click it, you may like to look this: Selenium WebDriver - determine if element is clickable (i.e. not obscured by dojo modal lightbox)

Actually, it may be difficult to check whether the element is clickable or not without actually clicking it.

2 Comments

if selenium tries to click and element and can't, does it notify the client by returning a certain value or anything?
It depends on the variety of conditions, we do have StaleElementExceptions, ElementNotVisibleException, etc but in some cases there may be possibility that a click has been captured but click event did not happened and we do not have any way to track that.
1

You can try the following if else condition

if(driver.findElement(By.xpath("--xpath of the clickable content")).isEnabled())
{
System.out.println("Element is clickable");
}
else
{
System.out.println("Element is not clickable");
}

2 Comments

You can do assertion instead of if else also
It being enabled does not directly mean it is clickable - for example, it maybe enabled but covered by another div.
0

One option is to do the following.

from selenium.common.exceptions import WebDriverException    
try:
  element.click()
  # add to list of clickable elements
except WebDriverException:
  print "Element is not clickable"

1 Comment

Really? If I ask you "How can I know if my phone is water-proof?", would you answer something like "Put it into the water and check"... :) Clicking on an element may navigate to another page, and if this happens, it won't matter if element was clickable or not.
0
try:
   WebDriverWait(driver, 1).until(EC.element_to_be_clickable(loc))
except:
   # not clickable
else:
   # clickable

1 Comment

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?
-1

You can create a custom keyword for it, but if element is displayed and enabled is required, but it's not all the conditions that make element clickable.

Example of custom keyword in custom lib:

from selenium.webdriver.remote.webelement import WebElement as webelement

def web_element_is_clickable(self, webelement):
    return webelement.is_displayed() and webelement.is_enabled()

1 Comment

If element is displayed and enabled it doesn't necessarily mean that it's clickable, because it may be covered by another element, and when you try to click it you'll get ElementClickInterceptedError. Also you may get ElementNotInteractableError if element doesn't support interactions at all.
-3
desplegar = bot.find_element_by_xpath('//*[@id="login-button"]')
try:
    if desplegar.is_enabled:
        desplegar.click()
    else:
         break
except:
    desplegar = None

1 Comment

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.

Your Answer

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.