How to check if element is hidden in Selenium Python?

Selenium Python – Check if element is hidden

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

To check if an element is hidden 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 hidden, then the function returns False, else it returns True.

Now, we can use is_displayed() method with not logical operator as an expression that returns True when the element is hidden, or False when the element is not hidden.

not element.is_displayed()

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

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

Examples

1. Scenario where Element is hidden

In this example, we shall consider loading the HTML file at path https://pythonexamples.org/tmp/selenium/index-14.html . The contents of this HTML file is given below.

<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>
Copy

In the following program, we initialize a driver object, load the specified URL, find the element with id="child3", and check if the element is hidden or not using is_displayed() method of the element object.

Since the element is already hidden using HTML hidden attribute, the condition evaluates to True, and the if-block is run.

Python Program

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

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

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-14.html')

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

# Check if the element is hidden
if not element.is_displayed():
    print("The element is hidden.")
else:
    print("The element is not hidden.")

# Close the driver
driver.quit()
Copy

Output

The element is hidden.

2. Scenario where Element is not hidden

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 hidden or not.

Since the element is visible when the webpage is loaded in a web browser, the if condition in the program evaluates to False, and the else-block is run.

Python Program

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

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

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-14.html')

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

# Check if the element is hidden
if not element.is_displayed():
    print("The element is hidden.")
else:
    print("The element is not hidden.")

# Close the driver
driver.quit()
Copy

Output

The element is not hidden.

3. Scenario where Element is hidden by CSS

In this example, we shall consider loading the HTML file at path https://pythonexamples.org/tmp/selenium/index-15.html . The contents of this HTML file is given below.

The div with id="child1" is hidden using CSS.

<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>
Copy

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

Python Program

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

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

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-15.html')

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

# Check if the element is hidden
if not element.is_displayed():
    print("The element is hidden.")
else:
    print("The element is not hidden.")

# Close the driver
driver.quit()
Copy

Output

The element is hidden.

Summary

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

Related Tutorials

Quiz on Selenium

Q1. Which of the following is not a popular web driver used in Selenium Python?

Not answered

Q2. What is the method used to find web elements 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. What is the method used to navigate back to the previous page in Selenium Python?

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered
Code copied to clipboard successfully 👍