Efficiently Checking for Non-Existing Elements in Selenium with Python

Efficiently Checking for Non-Existing Elements in Selenium with Python

3 July 2024 Stephan Petzl Leave a comment QA

When conducting automated tests using Selenium WebDriver in Python, one common challenge is verifying that a specific element does not appear after certain actions. Traditional methods involving exception handling can be time-consuming and inefficient. This article provides an efficient solution to check for non-existing elements without the performance overhead of exceptions.

Problem Overview

Consider the scenario where you need to assert that a popup does not appear after a specific action. The initial approach might involve exception handling:

try:
    self.driver.find_element_by_id("fancybox-close").click()
except Exception as e:
    print("No popup")

While this works for checking if the popup exists, it becomes inefficient when asserting its absence, as exception handling in Python can be slow, causing tests to run significantly longer.

Optimal Solution

A more efficient method involves using Selenium’s find_elements() method, which returns a list of elements. If the list is empty, the element does not exist, and no exception handling is needed. Here’s an example:

my_element_list = driver.find_elements_by_id("fancybox-close")
if not my_element_list:
    print("No popup")
else:
    if my_element_list[0].is_displayed():
        print("Popup is visible")
    else:
        print("Popup is hidden")

This method is efficient because it avoids the overhead of exception handling by leveraging Selenium’s ability to return empty lists when elements are not found.

Additional Techniques

Here are a few more techniques to optimize element checking:

  • Implicit Waits: Setting an implicit wait to zero can avoid unnecessary waiting times for elements that do not appear.
  • Explicit Waits: Use explicit waits to wait for specific conditions, such as element visibility.
  • JavaScript Executor: For elements that are particularly unreliable, using JavaScript can bypass the need for the element to be fully rendered.

Example of Explicit Wait

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID, "fancybox-close")))
element.click()

Conclusion

By using the find_elements() method and combining it with implicit or explicit waits, you can efficiently check for the non-existence of elements in Selenium with Python. This approach reduces the test execution time significantly compared to traditional exception handling methods.

For those looking to further streamline their automated testing process, especially for mobile applications, consider using Repeato. Repeato is a no-code test automation tool for iOS and Android that utilizes computer vision and AI to create, run, and maintain tests quickly and efficiently. Its ease of setup and use makes it an excellent choice for quality assurance teams.

For more detailed guides and documentation, visit our documentation page.

Like this article? there’s more where that came from!