How to check if an element is clickable or not using Selenium WebDriver
Asked Answered
S

7

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.

Supranatural answered 14/6, 2015 at 6:47 Comment(1)
C
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.

Cribble answered 14/6, 2015 at 7:4 Comment(0)
C
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.

Corcyra answered 14/6, 2015 at 14:43 Comment(2)
if selenium tries to click and element and can't, does it notify the client by returning a certain value or anything?Improvisator
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.Corcyra
C
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");
}
Criseyde answered 14/6, 2015 at 9:25 Comment(2)
You can do assertion instead of if else alsoGhassan
It being enabled does not directly mean it is clickable - for example, it maybe enabled but covered by another div.Grassplot
A
1

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"
Annadiana answered 29/5, 2020 at 15:28 Comment(0)
A
0
try:
   WebDriverWait(driver, 1).until(EC.element_to_be_clickable(loc))
except:
   # not clickable
else:
   # clickable
Athodyd answered 16/10, 2022 at 1:7 Comment(1)
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?Knowing
M
-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()
Murtagh answered 11/6, 2020 at 10:4 Comment(0)
G
-3
desplegar = bot.find_element_by_xpath('//*[@id="login-button"]')
try:
    if desplegar.is_enabled:
        desplegar.click()
    else:
         break
except:
    desplegar = None
Grantley answered 29/11, 2020 at 23:5 Comment(1)
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.Examination

© 2022 - 2024 — McMap. All rights reserved.