How to click on an image using Selenium in Java?
Last Updated :
13 Nov, 2024
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.
Prerequisites
A supported browser, such as Google Chrome or Mozilla Firefox, is installed, along with its corresponding driver.
Import Required Libraries
You must import the necessary classes to interact with the web elements using Selenium. Below are the key imports you will need:
package seleniumdemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
Steps to Click on Image using Selenium
Step 1: Open Inspect Element
First step is to open any website which you want to click through the image, next right click on it and open the "Inspect element" option.
Inspected ElementsStep 2: Get Image Source
Once the inspect options open we will have to find the code for the location of the image, in this case following is the code for location of the image.
Step 3: Copy Element By.className:
Once the image source is located we will have to right click on it and then go to Copy > By.className. this will give us the className of the image which is used for accessing the image in selenium using java.
Step 4: Code to Click on Image:
Following is the code for clicking the image in java using selenium, simply change the details of the code such as website URL and XPath of the image in the code:
HandleImage1.java
package seleniumpractice;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class HandleImage1 {
public static void main(String[] args) {
// Set the path to ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); // Replace with the correct path
// Initialize ChromeDriver
WebDriver driver = new ChromeDriver();
// Maximize the browser window
driver.manage().window().maximize();
// Navigate to GeeksforGeeks website
driver.navigate().to("https://www.geeksforgeeks.org/");
// Initialize WebDriverWait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// Try to click the GeeksforGeeks logo
try {
// Wait for and click on the GeeksforGeeks logo
WebElement geeksforgeeks = driver.findElement(By.className("headerMainLogo"));
geeksforgeeks.click();
System.out.println("GeeksforGeeks logo clicked successfully.");
} catch (Exception e) {
System.out.println("GeeksforGeeks logo not found or could not be clicked.");
} finally {
// Close the browser
driver.quit();
}
}
}
Explanation
- System.setProperty(): This method is used to set the path for the browser driver (in this case, ChromeDriver).
- ChromeDriver: This is an implementation of the WebDriver interface for Google Chrome.
- driver.get(): Opens the specified URL in the browser.
- driver.findElement(): This method locates the image using the ClassName provided (you can also use other locators like CSS selectors or ID).
- imageElement.click(): Once the image is located, the click() method is called to interact with it.
Output
Output of handling ImageConclusion
Clicking on an image using Selenium in Java is straightforward once you have identified the element properly. By using different locators, such as XPath, CSS selectors, or ID, you can interact with the image on the webpage. This approach can be used to automate various test scenarios where images are clickable or trigger specific events.
Similar Reads
How to Click on a Hyperlink Using Java Selenium WebDriver? An open-source tool that is used to automate the browser is known as Selenium. Automation reduces human effort and makes the work comparatively easier. There are numerous circumstances in which the user wants to open a new page or perform a certain action with the click of the hyperlink. In this art
4 min read
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 Perform Right-Click using Java in Selenium? While automating a website for testing there is always required to perform some right-click or other user actions on the page. Â These user actions are one of the most commonly used actions during automation, so selenium provides a way to perform these user actions by the Actions class. How to Perfor
2 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
How to find the src of an image in Selenium java? Selenium WebDriver is a popular automation tool used for testing web applications across different browsers. One common task in web automation is extracting information from web elements, such as retrieving the src attribute of an image. The src attribute holds the URL of the image, and Selenium pro
4 min read
How to Run Gecko Driver in Selenium Using Java? Selenium is a well-known software used for software testing purposes. It consists of three parts: Selenium IDE, Selenium WebDriver, and Selenium Grid. Selenium WebDriver is the most important. Using WebDriver, online website testing can be done. There are three main WebDriver implementations:ChromeD
5 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 Handle Alert in Selenium using Java? Imagine filling out a form online and accidentally missing some information. You only know if you made a mistake if the website tells you somehow, like with a pop-up message. This article explains what those pop-up messages are called in Selenium (alerts) and how to deal with them in your automated
5 min read
How to check if an image is displayed on page using Selenium? 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
3 min read
How to download File in Selenium Using java Automating the file download process is essential in web automation testing, especially for validating functionalities involving document downloads such as PDFs, images, or CSV files. However, Selenium WebDriver doesnât directly handle file downloads. To overcome this limitation, we can configure th
2 min read