Verify Image Presence in Web Page using Selenium WebDriver
JavaScript executor is one of the most useful features of Selenium WebDriver. It allows Selenium user to perform many complex scenarios easily. This post covers one such important scenario. Selenium verify image present example using WebDriver. We have used JavaScript executor in WebDriver Java code here to verify if image is present in a web page.
Perform below steps to see how this can be done.
- Find a web page which contains a broken image. Or you can create it easily in your local system.
- Open your class file and write a code to locate an image such as below. You can use different image attributes(Eg. alt, name etc.) to locate image using XPath.
1WebElement ImageFile = driver.findElement(By.xpath("//img[contains(@id,'Test Image')]")); - Write a JavaScript executor code to verify if image is present in page. It can be done as shown in below code.
1Boolean ImagePresent = (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", ImageFile); - Write a code to print desired output as per image presence state.
12345if (!imageLoaded1) {System.out.println("Image not displayed.");} else {System.out.println("Image displayed.");} - Done. Your method should look like below after performing above steps.
123456789101112131415@Testpublic void CheckImage() throws Exception {driver.get(baseUrl);WebElement ImageFile = driver.findElement(By.xpath("//img[contains(@id,'Test Image')]"));Boolean ImagePresent = (Boolean) ((JavascriptExecutor)driver).executeScript("return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0", ImageFile);if (!ImagePresent){System.out.println("Image not displayed.");}else{System.out.println("Image displayed.");}} - Run your test. It should identify if image is displayed in web page and will print appropriate result in console.
We hope the above Selenium verify image present example would be helpful to you in your test automation activities. Let us know this works out for you.
Comments
I think the best is to use isDisplayed method
Not sure how to do it. Can you please share how it can be done?
its not working
I tried using this script, but it doesn’t seem to work at all. All i get returned from the script is null, no matter what i do. Help?
You can use a try catch block.
The syntax is :
try {
driver.findElement(By.xpath(“xpath of the element”));
} catch (NoSuchElementException e) {
// code or print statements if an exception is found.
}
It worked fine for me….
I also get jave.null pointer exception. I have tried so many ways but still i get same exception. Please someone give the solution for this.
WebElement ImageFile = driver.findElement(By.id(“imageid”));
if (!ImagePresent)
{
System.out.println(“Image displayed.”);
}
else
{
System.out.println(“Image not displayed.”);
}
}
http://www.w3.org/TR/webdriver/#determining-visibility
Nice description !!!! Worth reading
@Test
public void CheckImage() throws Exception {
driver.get(baseUrl);
if (!isDisplay(By.xpath(“//img[contains(@id,’Test Image’)]”)))
{
System.out.println(“Image not displayed.”);
}
else
{
System.out.println(“Image displayed.”);
}
public boolean isDisplay(By by){
try{
WebElement elementDisply= driver.findElement(by);
elementDisply.isdisplayed();
Reporters.log(“element displayed successfully”);
return true;
}
catch(Excception e){
Reporters.log(“element not displayed “);
return false;
}
}
while we have broken images where the locator is validated but the actual image may not exist on the page. Then we need to determine the container height and width by which we can ensure that the image is displayed on the page.
By the above methods used by you we will get that the image is displayed as it will check only for identifier.
System.out.println(“Image displayed.”);
why to use System.out.println
HI guys,
i have to verify the banner in home , can please tell how to verify the banner
boolean isDisplayed = driver.findElement(By.xpath(“//img[contains(@id,’Test Image’)]”)).IsDisplayed();
How Do I access elements inside the canvas using selenium webdriver?
boolean imagepresent= driver.findElement(By.xpath(“html/body/div[2]//img”)).isDisplayed();
System.out.println(imagepresent);
boolean expected = true;
Assert.assertEquals(imagepresent,expected);
Simple and perfect!
– Use ‘is Element Displayed’ and return type t0 Boolean which will return True / False based on Image Presence
Hello, can you please help me. I also get jave.null pointer exception. I have tried so many ways but still i get same exception error . Please someone give the solution for this. my code mention in bleow
WebElement ImageFile = openbrowser.getDriver().findElement(By.xpath(“//*[@id=’form-group-photo_id_document’]/div[3]”));
System.out.println(ImageFile);
Boolean ImagePresent = (Boolean) ((JavascriptExecutor)openbrowser.getDriver()).executeScript(“return arguments[0].complete && typeof arguments[0].naturalWidth != \”undefined\” && arguments[0].naturalWidth > 0″, ImageFile);
System.out.println(ImagePresent);
Thread.sleep(3000);
if (!ImagePresent)
{
System.out.println(“Image not displayed.”);
}
else
{
System.out.println(“Image displayed.”);
}
}
For Firefox below code not working. please do needful
Boolean ImagePresent = (Boolean) ((JavascriptExecutor) driver).executeScript(
“return arguments[0].complete && typeof arguments[0].naturalWidth != \”undefined\” && arguments[0].naturalWidth > 0″,
ImageFile);
this code is working as expected but you need to change the xpath as per your image
how to get thumbnail