How to check if element is visible in Selenium Python?

Selenium Python – Check if element is visible

In this tutorial, you will learn how to check if a given element in the page is visible or not, using Selenium in Python.

To check if an element is visible in Selenium Python, get the element using a By strategy and locator string, and call the is_displayed() method on the element object. If the element is visible, then the function returns True, else it returns False.

element.is_displayed()

We can use a Python if-else statement and the above expression as if-condition, as shown in the following.

if element.is_displayed():
    print("The element is visible.")
else:
    print("The element is not visible.")

Examples

1. Element is not visible

Consider the following HTML file. The div with id="child3" is hidden, meaning the div is not visible. On the other hand the other two divs with id="child1" and id="child2" are just normal and are visible.

index.html

<html>
 <body>
  <h2>Hello User!</h2>
  <div id="parent">
    <div id="child1">This is child 1.</div>
    <div id="child2">This is child 2.</div>
    <div id="child3" hidden>This is child 3.</div>
  </div>
 </body>
</html>
Selenium Python - Check if element is visible - example input page

In the following program, we initialize a driver, then we load the index.html page running on our local server, or you may give the URL of the page you are interested in. Find the element with id="child3", and check if the element is visible or not using is_displayed() method of the element object.

Python Program

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# Setup chrome driver
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

# Navigate to the url
driver.get('http://127.0.0.1:5500/index.html')

# Find an element by its ID
element = driver.find_element(By.ID, 'child3')

# check if the element is visible
if element.is_displayed():
    print("The element is visible.")
else:
    print("The element is not visible.")

# Close the driver
driver.quit()

Output

Selenium Python - Check if element is visible - example 1 output

2. Element is visible

In this example, we shall load the same HTML file as in the previous example. And we shall check if the element with id="child2" is visible or not.

Python Program

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# Setup chrome driver
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

# Navigate to the url
driver.get('http://127.0.0.1:5500/index.html')

# Find an element by its ID
element = driver.find_element(By.ID, 'child2')

# check if the element is visible
if element.is_displayed():
    print("The element is visible.")
else:
    print("The element is not visible.")

# Close the driver
driver.quit()

Output

Selenium Python - Check if element is visible - example 2 output

3. Element is not visible by CSS

Consider the following HTML file. The div with id="child1" is hidden using CSS, meaning the div is not visible.

index.html

<html>
 <head>
  <style>
    #child1 {
      display: none;
    }
  </style>
 </head>
 <body>
  <h2>Hello User!</h2>
  <div id="parent">
    <div id="child1">This is child 1.</div>
    <div id="child2">This is child 2.</div>
    <div id="child3" hidden>This is child 3.</div>
  </div>
 </body>
</html>
Element is hidden using CSS

In the following program, we check if the element with id="child1" is visible or not.

Python Program

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# Setup chrome driver
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)

# Navigate to the url
driver.get('http://127.0.0.1:5500/index.html')

# Find an element by its ID
element = driver.find_element(By.ID, 'child1')

# check if the element is visible
if element.is_displayed():
    print("The element is visible.")
else:
    print("The element is not visible.")

# Close the driver
driver.quit()

Output

Selenium Python - Check if element is visible - example 3 output

Summary

In this Python Selenium tutorial, we have given instructions on how to check if given element is visible or not in the webpage, with example programs.

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. What is a web driver in Selenium Python?

Not answered

Q3. Which of the following is not a method to wait for a web element to load in Selenium Python?

Not answered

Q4. Which of the following is not a commonly used assertion method in Selenium Python?

Not answered

Q5. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered