Selenium is a portable software testing framework for web applications that supports multiple languages including Java, C#, Ruby, and Python.
In this shot, we discuss how to click an image using Selenium in Java.
To begin, let’s add the following dependency to pom.xml
.
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.3.0</version>
</dependency>
click()
methodThe click()
method is used to click on any web element.
In order to click on an image, we need to follow the steps below:
click()
method on the element.import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class Main{public static void main(String[] args) {System.setProperty("webdriver.gecko.driver","path to driver");String url = "https://educative.io/";WebDriver driver = new FirefoxDriver();driver.get(url);String cssSelectorForImage = "#__next > div.ed-grid > div.ed-grid-main > div > div.w-full.-mt-2.sm\\:mt-0 > div > div.w-full.lg\\:w-3\\/4.mt-12 > div > div > div > div:nth-child(1) > svg";driver.findElement(By.cssSelector(cssSelectorForImage)).click();driver.close();}}
url
to retrieve.url
using the get()
method.findElement()
method. We then invoke the click()
method on the element object.Free Resources