How to check if an image is displayed on page using Selenium?
Last Updated :
15 Oct, 2024
When it comes to web automation and testing, It is important to confirm that a picture has been rendered on a particular page. This is because bad content loading is regarded as a big issue in the good user interface. Users can easily get frustrated with missing or broken images, hence the need for testers and developers to check for the availability of the images.
In this article, we will explore how to check if an image is displayed on a webpage using Selenium in Java.
Prerequisites
How to check if an image is displayed on page using Selenium?
Following are the steps to check if an image is displayed on a page using Selenium:
Step 1. Set Up the Environment
Start by setting up the necessary environment for Selenium WebDriver in Java. You need to add the Selenium WebDriver dependency and the browser-specific driver (e.g., ChromeDriver) to your project.
Step 2. Navigate to the Webpage
After initializing the WebDriver, navigate to the target webpage that contains the image you want to check.
driver.get("https://www.geeksforgeeks.org");
Step 3. Locate the Image Element
Use the appropriate locator to identify the image on the webpage. In most cases, the `img` tag is used, and you can locate it using its ID, class, or other attributes.
WebElement imageElement = driver.findElement(By.xpath("//img[@id='exampleImage']"));
Step 4. Check if the Image is Displayed
Selenium provides a method called `isDisplayed()` that returns true if the element (in this case, the image) is visible and rendered on the page:
boolean isImageDisplayed = imageElement.isDisplayed();
if (isImageDisplayed) {
System.out.println("Logo present");
} else {
System.out.println("Logo NOT present);
}
Step 5. Close the WebDriver
driver.quit();
Syntax
driver.find_element(By.xpath, "//img[@src='https://media.geeksforgeeks.org/gfg-gg-logo.svg']")
Example to check if an image is displayed on page using Selenium
Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ImageVisibilityCheck {
public static void main(String[] args)
{
// Set the path for the ChromeDriver manually
System.setProperty("webdriver.chrome.driver",
"path/to/chromedriver");
// Instantiate the ChromeDriver
WebDriver driver = new ChromeDriver();
try {
// Navigate to the desired URL
driver.get("https://geeksforgeeks.org");
// Locate the image using its XPath
WebElement image = driver.findElement(By.xpath(
"//img[@src='https://media.geeksforgeeks.org/gfg-gg-logo.svg']"));
// Check if the image is displayed
if (image.isDisplayed()) {
System.out.println("Logo present");
}
else {
System.out.println("Logo NOT present");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Close the browser
driver.quit();
}
}
}
Output
OutputConclusion
To check if an image is displayed on a webpage using Selenium, first locate the image element with a method like find_element_by_xpath()
. Then, use the is_displayed()
method to verify its visibility. If the image is visible, it returns True
, otherwise False
. This method ensures you can confirm whether the image is rendered properly on the page.
Similar Reads
How to disable images in chrome using Selenium java? Disabling images in Chrome during automated testing can enhance performance and speed up your Selenium tests. This is particularly useful when dealing with large web pages or when you want to focus on specific elements without the distraction of images. In this guide, we'll walk you through how to d
2 min read
How to click on an image using Selenium in Java? Selenium, a popular tool for automating web application testing across browsers, often requires image interaction. In this article we will discuss how to clicking on image using Selenium in Java.PrerequisitesJava Development Kit (JDK) installed.Selenium WebDriver library added to your project.A supp
3 min read
How to get the total number of checkboxes in a page using Selenium? In the world of automated testing, Selenium is a powerful tool that helps automate web browsers. One common task during web testing is determining the number of specific elements on a page, such as checkboxes. This can be useful for validating the presence and quantity of checkboxes on a form or any
3 min read
How to check if any Alert exists using Selenium with Java? Checking if an alert exists using Selenium with Java is a common scenario when testing web applications. Alerts are pop-up windows generated by the browser to notify users of important messages, such as errors, warnings, or confirmations. An alert window is a pop-up box that is shown over the webpag
4 min read
How to Handle iframe in Selenium with Java? In this article, we are going to discuss how to handle the iframe in Selenium with Java. The following 6 points will be discussed.Table of ContentWhat are iframes in Selenium?Difference between frame and iframe in SeleniumSteps to Identify a Frame on a Page?How to Switch Over the Elements in iframes
11 min read
How to download Google Image Using java Selenium? Downloading images from Google using Java Selenium is a task that can come in handy for various automation projects. Whether you're building a dataset for machine learning, collecting images for research, or simply want to automate the process of downloading images, Selenium provides a robust soluti
5 min read