How to check if image is visible in Selenium Python?

Selenium Python – Check if image is visible

In this tutorial, you shall learn how to check if a specific image element is visible or not, using Selenium in Python language.

To check if an image element is visible in Selenium Python, you can call the is_displayed() method on the image object.

image.is_displayed()

This method call returns a boolean value of True if the image is visible, or False if the image is hidden.

Examples

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

<html>
<body> 
    <h3>Hello World</h3> 
    <img id="image1" src="https://pythonexamples.org/wp-content/uploads/2023/07/pe4-2.png" alt="My sample image 1">
    <img id="image2" src="https://pythonexamples.org/wp-content/uploads/2023/06/If.png" alt="My sample image 2">
    <img id="image3" src="https://pythonexamples.org/wp-content/uploads/2023/04/test_image-1.jpg" alt="My sample image 3" hidden>
</body>
</html>
Copy

1. Check if image with id=”image1″ is visible

In the following program, we initialize a driver object, load the specified URL, find the image element with id=”image1″, and check if this image is visible using WebElement.is_displayed() method.

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-30.html')

# Find the image
image_1 = driver.find_element(By.ID, 'image1')

# Check if image is visible
if image_1.is_displayed():
    print("The image is visible.")
else:
    print("The image is not visible.")

# Close the driver
driver.quit()

Output

The image is visible.

2. Check if image with id=”image3″ is visible

In the following program, we shall check if the image element with id=”image3″ is visible or not. From the given HTML of webpage, the image is hidden. Therefore, is_displayed() method returns False.

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-30.html')

# Find the image
image_3 = driver.find_element(By.ID, 'image3')

# Check if image is visible
if image_3.is_displayed():
    print("The image is visible.")
else:
    print("The image is not visible.")

# Close the driver
driver.quit()

Output

The image is not visible.

Summary

In this Python Selenium tutorial, we have seen how to check if the given image element is visible or not using WebElement.is_displayed() method, 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 interact with web elements in Selenium Python?

Not answered

Q3. What is the method used to navigate back to the previous page in Selenium Python?

Not answered

Q4. What is the method used to switch to a different frame in Selenium Python?

Not answered

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

Not answered
Code copied to clipboard successfully 👍