- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if an image is displayed on page using Selenium?
We can check if an image is displayed on page with Selenium. To verify an image we shall take the help of Javascript Executor. We shall utilize executeScript method to execute the Javascript commands in Selenium.
Then pass the command return arguments[0].complete && typeof arguments[0].naturalWidth != \"undefined\" && arguments[0].naturalWidth > 0 as a parameter to that method. Moreover, the statement import org.openqa.selenium.JavascriptExecutor has to be added in code.
Syntax
WebElement i = driver.findElement(By.xpath("//img[@title='Tutorialspoint']")); Boolean p = (Boolean) ((JavascriptExecutor)driver) .executeScript("return arguments[0].complete " + "&& typeof arguments[0].naturalWidth != \"undefined\" " + "&& arguments[0].naturalWidth > 0", i);
Let us check if the below image is present on page −
Example
Code Implementation.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class ImageChk{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); //identify image WebElement i = driver.findElement (By.xpath("//img[@title='Tutorialspoint']")); // Javascript executor to check image Boolean p = (Boolean) ((JavascriptExecutor)driver) .executeScript("return arguments[0].complete " + "&& typeof arguments[0].naturalWidth != \"undefined\" " + "&& arguments[0].naturalWidth > 0", i); //verify if status is true if (p) { System.out.println("Logo present"); } else { System.out.println("Logo not present"); } driver.quit(); } }
Output
- Related Articles
- How to verify if an element is displayed on screen in Selenium?
- How can I check if some text exist or not in the page using Selenium?
- How to check if an Image has crop applied using FabricJS?
- Check if mirror image of a number is same if displayed in seven segment displays in Python
- How to check if an Image object intersects with another object using FabricJS?
- How to perform scrolling action on page in Selenium?
- How to get the total number of radio buttons on a page using Selenium?
- How to Insert an Image in HTML Page?
- How to get content of entire page using Selenium?
- How to obtain the page title using Selenium webdriver?
- Check if number can be displayed using seven segment led in Python
- How to get page source as it is in browser using selenium?
- How to identify elements based on text visible on page in Selenium?
- How to set Page Load Timeout using C# using Selenium WebDriver?
- How to pick an image from an image gallery on Android using Kotlin?

Advertisements