How to Click on Image in Selenium Webdriver
โก Smart Summary
Click On Image in Selenium WebDriver requires CSS selector or XPath strategies because image elements do not expose link text. This article explains how to locate the image, trigger the click action, and verify that navigation succeeds.

Clicking an image in Selenium WebDriver is a common task when an icon, logo, or thumbnail behaves as a navigational link. Because images carry no visible text, traditional link-text locators fail and a more reliable selector strategy is required.
Accessing Image Links
Image links are anchors on web pages represented by an image which, when clicked, navigates the browser to a different window or page.
Because they are images, we cannot use the By.linkText() and By.partialLinkText() methods because image links have no link text at all.
In this case, we should resort to using either By.cssSelector or By.xpath. The first method is generally preferred because of its simplicity and readability.
In the example below, we will access the “Facebook” logo on the upper left portion of Facebook’s Password Recovery page.
We will use By.cssSelector and the element’s “title” attribute to access the image link. We will then verify that the browser has been taken to Facebook’s homepage.
Java Code Example
The following Selenium WebDriver snippet launches Chrome, opens the Facebook password recovery page, clicks the Facebook logo image, and asserts the resulting page title.
package newproject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyClass {
public static void main(String[] args) {
String baseUrl = "https://www.facebook.com/login/identify?ctx=recover";
System.setProperty("webdriver.chrome.driver", "G:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(baseUrl);
// click on the "Facebook" logo on the upper left portion
driver.findElement(By.cssSelector("a[title=\"Go to Facebook home\"]")).click();
// verify that we are now back on Facebook's homepage
if (driver.getTitle().equals("Facebook - log in or sign up")) {
System.out.println("We are back at Facebook's homepage");
} else {
System.out.println("We are NOT in Facebook's homepage");
}
driver.close();
}
}
Result
Best Practices for Clicking Images
A few habits make image-click automation more reliable across browsers and screen sizes.
- Prefer a stable attribute such as title, alt, aria-label, or data-* over deeply nested CSS paths.
- Add an explicit WebDriverWait for ElementToBeClickable before issuing the click.
- For lazy-loaded images, scroll the element into view with JavascriptExecutor first.
- If a transparent overlay intercepts the click, fall back to a JavaScript click on the underlying element.
- Validate navigation with driver.getTitle() or a unique element on the destination page.
How AI Improves Image Locator Strategies
AI-assisted testing tools such as Testim, Mabl, and Healenium analyze the DOM around an image and propose self-healing locators. When a class name or attribute changes after a release, the framework swaps in the most stable alternative automatically, which reduces flaky failures on icon-based navigation.
Engineers can also use ChatGPT or GitHub Copilot to generate WebDriver snippets, suggest robust CSS or XPath expressions for an image, and draft assertions for the page that loads after the click. Generated code should still be reviewed for correctness and for handling of waits and overlays.
Conclusion
Clicking image links in Selenium WebDriver is straightforward once you choose a stable locator. By.cssSelector with the title or alt attribute is the most readable approach, By.xpath is the fallback when the markup is awkward, and explicit waits make the script reliable across runs.

.png)
.png)