-4

I would like to catch a error every time I click an icon or an element that is not active.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

2
try:
    element.click()
except:
    print("error")
0
try:
    element.click()
except Exception as e:
        print(e)

you can print the exception thrown like this

PDHide
  • 18,113
  • 2
  • 31
  • 46
-1

Selenium won't be able to differentiate between active and inactive state of an element. But Selenium can distinguish the different states of a WebElement interms of:


This usecase

In short click() can fail majorly because of either:


Solution

You can catch NoSuchElementException using the following code block:

try:
    driver.find_element(By.CSS_SELECTOR, "a.class#id[attribute='value']").click()
except NoSuchElementException:
    print("No such element")
    

You can avoid ElementClickInterceptedException inducing WebDriverWait for the element_to_be_clickable() as follows:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "element_css"))).click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352