Selenium Python - Handling No such element exception
Are you not importing the exception?
from selenium.common.exceptions import NoSuchElementException
try:
elem = driver.find_element_by_xpath(".//*[@id='SORM_TB_ACTION0']")
elem.click()
except NoSuchElementException: #spelling error making this code not work as expected
pass
the way you are doing it is fine.. you are just trying to catch the wrong exception. It is named NoSuchElementException
not nosuchelementexception
You can see if the element exists and then click it if it does. No need for exceptions. Note the plural "s" in .find_elements_*
.
elem = driver.find_elements_by_xpath(".//*[@id='SORM_TB_ACTION0']")
if len(elem) > 0
elem[0].click()