5 Best Ways to Check the Status of an Element in a Page with Selenium in Python

πŸ’‘ Problem Formulation: When utilizing Selenium with Python for automated web testing, it’s crucial to ascertain the status of web elements. Developers need to confirm whether elements are displayed, enabled, selected, or meet specific conditions. For instance, before interacting with a button, we must determine if it’s visible and clickable. This article guides you through various solutions to check an element’s status, enhancing your testing scripts’ robustness and reliability.

Method 1: Checking Visibility with is_displayed()

An essential method in Selenium for checking if an element is visible on the page is is_displayed(). Visibility does not necessarily mean that the element is viewable by the user, but that it is present on the DOM and not hidden.

Here’s an example:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://example.com')
element = driver.find_element_by_id('visibleElement')
is_visible = element.is_displayed()
print(f'Is the element visible? {is_visible}')
driver.quit()

Output: Is the element visible? True

This code initializes a Selenium WebDriver for Chrome, navigates to ‘https://example.com’, then locates an element by its ID and checks if it is currently displayed on the page. The result is printed to the console and the browser is closed.

Method 2: Checking if Enabled with is_enabled()

Using is_enabled() method, you can verify if a web element is enabled and thus interactable. This is important for form elements where user interaction is required.

Here’s an example:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://example.com')
element = driver.find_element_by_id('submitButton')
is_enabled = element.is_enabled()
print(f'Is the submit button enabled? {is_enabled}')
driver.quit()

Output: Is the submit button enabled? True

This snippet does a similar setup but instead checks if a ‘submitButton’ is enabled, indicating that the user can click or interact with it.

Method 3: Checking Selection with is_selected()

For checkboxes or radio buttons, the is_selected() method is the right choice to determine if an element is selected. This ensures that tests verify the element’s state accurately.

Here’s an example:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://example.com')
checkbox = driver.find_element_by_id('agreeCheckbox')
is_checked = checkbox.is_selected()
print(f'Is the checkbox selected? {is_checked}')
driver.quit()

Output: Is the checkbox selected? False

Here, the WebDriver inspects a checkbox to see if it has been selected. This method is crucial for validating the state of form elements.

Method 4: Using Expected Conditions with WebDriverWait

Selenium’s WebDriverWait in combination with expected conditions allows for more complex state checks, such as waiting for an element to be clickable or visible.

Here’s an example:

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

driver = webdriver.Chrome()
driver.get('https://example.com')

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'submitButton')))
print(f'Is the submit button clickable? {element is not None}')
driver.quit()

Output: Is the submit button clickable? True

In the code above, WebDriverWait is utilized alongside an expected condition of ‘element_to_be_clickable’ to wait up to 10 seconds for a ‘submitButton’ to become clickable.

Bonus One-Liner Method 5: Check Existence with find_elements and Conditional

For a quick check to see if an element exists on a page without throwing an error, you can use find_elements which returns a list, then check its length.

Here’s an example:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://example.com')
elements = driver.find_elements_by_id('nonExistentElement')
print(f'Does the element exist? {"Yes" if elements else "No"}')
driver.quit()

Output: Does the element exist? No

This one-liner uses the fact that find_elements_by_id returns a list – if the element is not found, the list is empty, and thus the existence check returns “No”.

Summary/Discussion

  • Method 1: is_displayed(). Strengths: Quickly checks visibility. Weaknesses: Element may be off-screen.
  • Method 2: is_enabled(). Strengths: Confirms interactability. Weaknesses: Doesn’t guarantee visibility.
  • Method 3: is_selected(). Strengths: Essential for checkboxes/radio buttons. Weaknesses: Only applicable to selectable elements.
  • Method 4: WebDriverWait with expected conditions. Strengths: Advanced and thorough checks. Weaknesses: More complex syntax; requires understanding of expected conditions.
  • Bonus One-Liner Method 5: Existence with find_elements. Strengths: Simple and doesn’t throw exceptions. Weaknesses: Does not provide element’s state.