I have a python class to handle some basic selenium stuff. One of the methods is a wrapper to check if some element is present on the page:
from selenium.common.exceptions import TimeoutException, NoSuchElementException
...
def check_element(self, by, element):
try:
self.driver.find_element(by, element)
return True
except TimeoutException, NoSuchElementException:
return False
I call this method with something like this:
self._base.check_element((By.XPATH, '//*[@id="kernel_indicator_icon"]))
But when running the code I get an unexpected error:
...
File "/Users/adietz/Projects/Jenkins/selenium-tests/selenium_tests/tools/basicsuite.py", line 277, in check_element
self.driver.find_element(by, element)
File "/Users/adietz/Projects/Jenkins/venv_selenium/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 858, in find_element
'value': value})['value']
File "/Users/adietz/Projects/Jenkins/venv_selenium/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "/Users/adietz/Projects/Jenkins/venv_selenium/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element: //*[@id="kernel_indicator_icon" and @title="Kernel Idle"]
Why hasn't this exception been caught by the try-except
block above?
Straigh from the documentation
def check_element(self, by, element):
try:
self.driver.find_element(by, element)
return True
except (TimeoutException, NoSuchElementException) as e:
return False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.