I would like to catch a error every time I click an icon or an element that is not active.
Asked
Active
Viewed 44 times
3 Answers
0
try:
element.click()
except Exception as e:
print(e)
you can print the exception thrown like this
PDHide
- 15,234
- 2
- 19
- 35
-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
- 151,581
- 34
- 225
- 281