How to handle exceptions in Selenium Python WebDriver
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.
Exception handling is not new and it is the process of responding to the occurrence of exceptions that may happen during execution of our program.
For example, when we are trying to click on an element which is not at all displayed on the browser, will cause our program to throw exception altering the expected flow.
Addition to the flow alteration, exceptions can crash the system and forbid next valid statements to get executed.
The exception classes are available in selenium.common.exceptions package and can be imported like this
Syntax:
from selenium.common.exceptions import [Exception Name]
Example:
from selenium.common.exceptions import NoSuchElementException
For example, we have a html page in which we need to find element with id attribute ‘privacy-policy’.
<html> <body> <form id='sigupForm'> Username: <input name='username' type='text'/><br/> Password: <input name='password' type='password'/><br/> <input id='privacypolicy' name='privacypolicy' type='checkbox'>Accept Privacy Policy</input><br/> <input id='termsconditions' name='termscondition' type='checkbox'>Accept Terms and Condition</input><br/> <input type='button' value='Submit'></input> </form> </body> </html>
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException driver = webdriver.Firefox(executable_path="[Firefox driver path]") driver.get("[URL to open]") webElement = driver.find_element_by_id("privacy-policy") webElement.click()
But due to some changes id attribute has been changed to ‘privacypolicy’ in which case our tests will fail with below exception and our tests will fail.
File "ExceptionHandling.py", line 7, in
driver.find_element_by_id("privacy-policy");
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 341, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 855, in find_element
'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="privacy-policy"]
In order to ensure that our test fails with proper error message we need to capture ‘NoSuchElementException’ and print appropriate log message.
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException driver = webdriver.Firefox(executable_path="[Firefox driver path]") driver.get("[URL to open]") try: webElement = driver.find_element_by_id("privacy-policy") webElement.click() except NoSuchElementException as exception: print "Element not found and test failed"
Output:
allselenium$ python exception-handling.py Element not found and test failed allselenium$
6 Responses
thanks
Really Valuable thanks Man..!
With thanks! Valuable information!
Sir, can you post another blog for handling StaleElementReferenceException for selenium web driver in python?
Sure 🙂
Please check this article – http://allselenium.info/handle-stale-element-reference-exception-python-selenium/
and let me know your comments