is_displayed Method

The is_displayed() method in Selenium is used to check whether a web element is visible to the user on the webpage. This method returns True if the element is visible and False if it is hidden (e.g., using CSS display: none, visibility: hidden, or if it is outside the viewport).

Syntax:

element.is_displayed()

Example:

from selenium import webdriver
from selenium.webdriver.common.by import By

# Set up the driver (assuming you're using Chrome)
driver = webdriver.Chrome()

# Open a webpage with website URL
driver.get('https://www.google.com')

# Locate the element (e.g., a button or div)
element = driver.find_element(By.NAME, 'q')

# Check if the element is displayed
if element.is_displayed():
    print("The element is visible on the page.")
else:
    print("The element is not visible on the page.")

# Close the browser
driver.quit()

Explanation:

  • element.is_displayed() returns True if the element is visible to the user.
  • This method can be useful when you want to verify if an element is present on the page and is currently visible (not hidden or collapsed).