7 Common Selenium Python Exceptions And How To Handle Them

Selenium Python Exceptions And How To Handle Them

Selenium is one of the most popular open-source automation testing frame works used for web applications. It provides support for various programming languages, including Python. Selenium with Python is a powerful combination for web automation testing, but like any automation tool, it can encounter several exceptions during the testing process. These exceptions can cause your test scripts to fail, leading to delays in your testing process.

In this blog, we will discuss seven common Selenium Python exceptions you may encounter while working with Selenium for automation testing. We will also provide detailed explanations of what causes each of these exceptions and how to handle them effectively. By understanding and handling these exceptions, you can ensure that your automation process runs smoothly and efficiently on your local system or cloud platforms.

This blog is designed to help software testers, and automation engineers of all levels understand how to handle these exceptions and troubleshoot common issues that may arise during the testing process. With a little bit of practice and attention to detail, you can become an expert at identifying and handling these exceptions in your Selenium Python test scripts. Learning these exceptions ensures more robust and reliable automated testing. So, let’s dive into Selenium Python exceptions and how to handle them.

1. NoSuchElementException

Selenium Python Exceptions And How To Handle Them
Source: automationbro.hashnode.dev

NoSuchElementException is one of the most common exceptions in Selenium Python. It occurs when you want to locate an element that does not exist on the page. The reason for this exception can be several, like incorrect locator strategy, wrong element name, or even a slow page load.

To handle this exception, you can use the try-except block. Take a look at the example code snippet that demonstrates how to handle the NoSuchElementException exception.

from selenium.common.exceptions import NoSuchElementException

from selenium import webdriver

driver = webdriver.Chrome()

driver.get(“https://www.google.com”)

try:

element = driver.find_element_by_xpath(“//input[@name=’a1′]”)

element.send_keys(“Selenium Python”)

except NoSuchElementException:

print(“Element not found”)

In this code snippet, we are trying to locate an element with the name ‘a1’. Since there is no such element with this name, it will throw a NoSuchElementException exception. We have used the try-except block to handle this exception. In the except block, we are printing the message “Element not found.”

2. ElementNotVisibleException

Selenium Python Exceptions
Source: analityk.edu.pl

ElementNotVisibleException happens when you try to interact with an element that is present on the page but invisible to the user. This exception can be because the element is hidden behind another element or it is not visible due to some CSS properties.

To handle this exception, you can use the WebDriverWait class. Here is an example code snippet that demonstrates how to handle the ElementNotVisibleException exception.

from selenium.common.exceptions import ElementNotVisibleException

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium import webdriver

driver = webdriver.Chrome()

driver.get(“https://www.google.com”)

try:

element = WebDriverWait(driver, 10).until(

EC.visibility_of_element_located((By.XPATH, “//input[@name=’q’]”))

)

element.send_keys(“Selenium Python”)

except ElementNotVisibleException:

print(“Element not visible”)

In this code snippet, we are trying to locate an element with the name ‘q’. If the element is not visible, it will throw an ElementNotVisibleException exception. We have used the WebDriverWait class to wait until the element is visible on the page. In the except block, we are printing the message “Element not visible.”

3. StaleElementReferenceException

Source: blog.testproject.io

StaleElementReferenceException occurs when you try to interact with an element that is no longer attached to the DOM. This can happen when the page is reloaded, and the element is deleted or changed in some way.

To handle this exception, you can use the try-except block. Here is an example code snippet that demonstrates how to handle the StaleElementReferenceException exception.

from selenium.common.exceptions import StaleElementReferenceException

from selenium import webdriver

driver = webdriver.Chrome()

driver.get(“https://www.google.com”)

try:

element = driver.find_element_by_xpath(“//input[@name=’q’]”)

driver.refresh()

element.send_keys(“Selenium Python”)

except StaleElementReferenceException:

print(“Element no longer attached to the DOM”)

In this code snippet, we are trying to locate an element with the name ‘q’. After locating the element, we are refreshing the page. When we try to interact with the element after the page refreshes, it will throw a StaleElementReferenceException exception. We have used the try-except block to handle this exception. In the except block, we are printing the message “Element no longer attached to the DOM.”

4. TimeoutException

Source: usnews.com

TimeoutException occurs when a page takes too long to load or an element takes too long to appear on the page. This can happen due to network issues or a slow server.

To handle this exception, you can use the WebDriverWait class. Here is an example code snippet that demonstrates how to handle the TimeoutException exception.

from selenium.common.exceptions import TimeoutException

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium import webdriver

driver = webdriver.Chrome()

driver.get(“https://www.google.com”)

try:

element = WebDriverWait(driver, 10).until(

EC.presence_of_element_located((By.XPATH, “//input[@name=’q’]”))

)

element.send_keys(“Selenium Python”)

except TimeoutException:

print(“Page or Element not loaded”)

In this code snippet, we are trying to locate an element with the name ‘q’. If the page or element takes more than 10 seconds to load, it will throw a TimeoutException exception. We have used the WebDriverWait class to wait until the element is present on the page. In the except block, we are printing the message “Page or Element not loaded.”

5. ElementClickInterceptedException

ElementClickInterceptedException 1
Source: youtube.com

ElementClickInterceptedException occurs when you try to click on an element, but another element is covering it. This can happen when there are pop-ups, overlays, or advertisements on the page.

To handle this exception, you can use the WebDriverWait class. Here is an example code snippet that demonstrates how to handle the ElementClickInterceptedException exception.

from selenium.common.exceptions import ElementClickInterceptedException

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium import webdriver

driver = webdriver.Chrome()

driver.get(“https://www.google.com”)

try:

element = WebDriverWait(driver, 10).until(

EC.element_to_be_clickable((By.XPATH, “//button[@id=’popup-close’]”))

)

element.click()

except ElementClickInterceptedException:

print(“Element click intercepted”)

In this code snippet, we are trying to click on a pop-up close button. If another element is covering the button, it will throw an ElementClickInterceptedException exception. We have used the WebDriverWait class to wait until the button is clickable on the page. In the except block, we are printing the message “Element click intercepted.”

6. WebDriverException

WebDriverException is a generic exception that can occur due to various reasons, such as an unexpected browser crash, incorrect browser version, or an invalid browser path.

To handle this exception, you can use the try-except block. Here is an example code snippet that demonstrates how to handle the WebDriverException exception.

from selenium.common.exceptions import WebDriverException

from selenium import webdriver

try:

driver = webdriver.Chrome()

driver.get(“https://www.google.com”)

except WebDriverException:

print(“WebDriverException occurred”)

In this code snippet, we are trying to launch the Chrome browser and navigate to the Google homepage. If there is any issue with the browser, it will throw a WebDriverException exception. We have used the try-except block to handle this exception. In the except block, we are printing the message “WebDriverException occurred.”

7. InvalidSelectorException

InvalidSelectorException
Source: techcrunch.com

InvalidSelectorException occurs when you use an incorrect selector to locate an element. This can happen when you use an invalid CSS selector, XPath expression, or any other locator strategy.

To handle this exception, you can use the try-except block. Here is an example code snippet that demonstrates how to handle the InvalidSelectorException exception.

from selenium.common.exceptions import InvalidSelectorException

from selenium import webdriver

driver = webdriver.Chrome()

driver.get(“https://www.google.com”)

try:

element = driver.find_element_by_xpath(“//input[@name=’q’][“)

element.send_keys(“Selenium Python”)

except InvalidSelectorException:

print(“Invalid selector used”)

In this code snippet, we are trying to locate an element with an invalid XPath expression. It will throw an InvalidSelectorException exception. We have used the try-except block to handle this exception. In the except block, we are printing the message “Invalid selector used.”

LambdaTest is a cloud-based automation testing platform that helps you run automated tests on a range of browsers and devices, saving you time and effort. With access to 3000+ real browsers, OS and devices including the latest versions of Chrome, Firefox, Safari, Edge and more, gives you everything you need to test cross-platform with ease.

If you are interested what programming language is used for developing casino games, you shoud check out our blog post where we dive deeper into this topic.

Conclusion

In conclusion, handling exceptions is a crucial aspect of Selenium Python automation testing. In this blog, we have discussed seven common exceptions that can occur while working with Selenium Python. We have also provided solutions on how to handle these exceptions using techniques such as try-except blocks, WebDriverWait class, and more.

By understanding and implementing these solutions, you can handle these exceptions efficiently and ensure that your automation process runs smoothly. Remember, handling exceptions is essential for creating robust and reliable automation test scripts. So, always be prepared to handle these exceptions and ensure the success of your automation testing efforts.