How to check if an element is clickable or not usi

2020-07-13 07:20发布

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.

5条回答
▲ chillily
2楼-- · 2020-07-13 07:58

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");
}
查看更多
闹够了就滚
3楼-- · 2020-07-13 08:10

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.

查看更多
小情绪 Triste *
4楼-- · 2020-07-13 08:10

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()
查看更多
闹够了就滚
5楼-- · 2020-07-13 08:14

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"
查看更多
做个烂人
6楼-- · 2020-07-13 08:16

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.

查看更多
登录 后发表回答