+91 9790664230
kumar.arun211@gmail.com

How to handle exceptions in Selenium Python WebDriver

Blog to learn python and selenium

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:
arunkumar$ ~/Documents/Workspace/blog/Blog $ python 11-ExceptionHandling.py
Element not found and test failed
arunkumar$ ~/Documents/Workspace/blog/Blog $
arunkumar$ ~/Documents/Workspace/blog/Blog $

 

6 Responses

  1. Akhil Bharadwaj says:

    Sir, can you post another blog for handling StaleElementReferenceException for selenium web driver in python?

  2. Rahul Kumar says:

    Very interesting blog post.Quite informative and very helpful.This indeed is one of the recommended blog for learners.Thank you for providing such nice piece of article. I’m glad to leave a comment. Expect more articles in future. You too can check this Python tutorial for updated knowledge on Python.https://www.youtube.com/watch?v=HcsvDObzW2U

  3. suresh kumar says:

    Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
    python training in chennai

  4. Keturah Koeppe says:

    With thanks! Valuable information!

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2018 Copyrights. All Rights Reserved. Arunkumar Velusamy