---
description: Accessing Image links in web pages represented by an image that navigates to a different window or page when clicked navigates to a different window or page.
title: How to Click on Image in Selenium Webdriver
image: https://www.guru99.com/images/click-on-image-in-selenium-webdriver.png
---

 

[Skip to content](#main) 

**⚡ 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.

* 🖱️ **Core Action:** Use driver.findElement(By.cssSelector(…)).click() to perform a click on any image element rendered inside an anchor.
* 🎯 **Locator Choice:** By.linkText and By.partialLinkText do not work for images, so By.cssSelector or By.xpath are required.
* 🧪 **Verification Step:** Confirm a successful click by asserting the new page title with driver.getTitle() against the expected destination string.
* 🤖 **AI Assistance:** Modern AI plugins suggest stable CSS selectors and auto-heal locators when the application markup or attribute values change.
* 🛠️ **Common Pitfalls:** Hidden, lazy-loaded, or overlapped images often require explicit waits, scroll-into-view, or JavaScript click to register properly.

[ Read More ](javascript:void%280%29;) 

![Click on Image in Selenium](https://www.guru99.com/images/click-on-image-in-selenium-webdriver.png)

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.

[](https://www.guru99.com/images/image017%281%29.png)

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**

[](https://www.guru99.com/images/image018%282%29.png)

## 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.

### RELATED ARTICLES

* [UFT vs Selenium: Key Difference Between Them ](https://www.guru99.com/alm-qtp-selenium-difference.html "UFT vs Selenium: Key Difference Between Them")
* [How to Use Log4j in Selenium? ](https://www.guru99.com/tutorial-on-log4j-and-logexpert-with-selenium.html "How to Use Log4j in Selenium?")
* [Selenium with Cucumber (BDD Framework Tutorial) ](https://www.guru99.com/using-cucumber-selenium.html "Selenium with Cucumber (BDD Framework Tutorial)")
* [Right Click and Double Click in Selenium ](https://www.guru99.com/double-click-and-right-click-selenium.html "Right Click and Double Click in Selenium")

## 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.

## FAQs

🖱️ How do you click on an image in Selenium WebDriver?

Locate the image with By.cssSelector or By.xpath using a stable attribute such as title, alt, or aria-label, then call .click() on the returned WebElement. Verify the result with driver.getTitle() or a unique element on the destination page.

🤖 Can AI generate the CSS selector for an image automatically?

Yes. Tools such as Testim, Mabl, and ChatGPT inspect the surrounding DOM and suggest a stable selector based on title, alt, aria-label, or data attributes. Engineers should still review the generated locator for uniqueness and resilience.

🧠 What is a self-healing locator and does it work on images?

A self-healing locator is an AI feature that swaps in an alternative selector when the original fails. Frameworks like Healenium and Mabl observe attribute changes around an image and pick the most stable replacement, which reduces flaky tests after UI refreshes.

⚖️ Why do By.linkText and By.partialLinkText fail for images?

Both locators match on the visible text inside an anchor. An image link wraps an img tag and exposes no text node, so the match always returns nothing. Use By.cssSelector or By.xpath with title, alt, or aria-label instead.

🛠️ What if the image click is intercepted by another element?

If an overlay, modal, or tooltip blocks the image, scroll it into view with JavascriptExecutor and add a WebDriverWait for ElementToBeClickable. As a fallback, execute a JavaScript click on the WebElement to bypass the overlapping layer.

🧪 How do you verify a successful image click?

Assert the new page title with driver.getTitle(), the current URL with driver.getCurrentUrl(), or the presence of a unique element on the destination page. A combination of these checks gives the test confidence that the click really triggered navigation.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/click-on-image-in-selenium-webdriver.png","url":"https://www.guru99.com/images/click-on-image-in-selenium-webdriver.png","width":"700","height":"250","caption":"How to Click on Image in Selenium Webdriver","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/click-on-image-in-selenium.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/selenium","name":"Selenium"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/click-on-image-in-selenium.html","name":"How to Click on Image in Selenium Webdriver"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/click-on-image-in-selenium.html#webpage","url":"https://www.guru99.com/click-on-image-in-selenium.html","name":"How to Click on Image in Selenium Webdriver","dateModified":"2026-06-15T18:19:58+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/click-on-image-in-selenium-webdriver.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/click-on-image-in-selenium.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta","url":"https://www.guru99.com/author/admin","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","url":"https://www.guru99.com/images/krishna-rungta-v2-120x120.png","caption":"Krishna Rungta","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Selenium","headline":"How to Click on Image in Selenium Webdriver","description":"Accessing Image links in web pages represented by an image that navigates to a different window or page when clicked navigates to a different window or page.","keywords":"selenium","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/admin","name":"Krishna Rungta"},"dateModified":"2026-06-15T18:19:58+05:30","image":{"@id":"https://www.guru99.com/images/click-on-image-in-selenium-webdriver.png"},"copyrightYear":"2026","name":"How to Click on Image in Selenium Webdriver","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How do you click on an image in Selenium WebDriver?","acceptedAnswer":{"@type":"Answer","text":"Locate the image with By.cssSelector or By.xpath using a stable attribute such as title, alt, or aria-label, then call .click() on the returned WebElement. Verify the result with driver.getTitle() or a unique element on the destination page."}},{"@type":"Question","name":"Can AI generate the CSS selector for an image automatically?","acceptedAnswer":{"@type":"Answer","text":"Yes. Tools such as Testim, Mabl, and ChatGPT inspect the surrounding DOM and suggest a stable selector based on title, alt, aria-label, or data attributes. Engineers should still review the generated locator for uniqueness and resilience."}},{"@type":"Question","name":"What is a self-healing locator and does it work on images?","acceptedAnswer":{"@type":"Answer","text":"A self-healing locator is an AI feature that swaps in an alternative selector when the original fails. Frameworks like Healenium and Mabl observe attribute changes around an image and pick the most stable replacement, which reduces flaky tests after UI refreshes."}},{"@type":"Question","name":"Why do By.linkText and By.partialLinkText fail for images?","acceptedAnswer":{"@type":"Answer","text":"Both locators match on the visible text inside an anchor. An image link wraps an img tag and exposes no text node, so the match always returns nothing. Use By.cssSelector or By.xpath with title, alt, or aria-label instead."}},{"@type":"Question","name":"What if the image click is intercepted by another element?","acceptedAnswer":{"@type":"Answer","text":"If an overlay, modal, or tooltip blocks the image, scroll it into view with JavascriptExecutor and add a WebDriverWait for ElementToBeClickable. As a fallback, execute a JavaScript click on the WebElement to bypass the overlapping layer."}},{"@type":"Question","name":"How do you verify a successful image click?","acceptedAnswer":{"@type":"Answer","text":"Assert the new page title with driver.getTitle(), the current URL with driver.getCurrentUrl(), or the presence of a unique element on the destination page. A combination of these checks gives the test confidence that the click really triggered navigation."}}]}],"@id":"https://www.guru99.com/click-on-image-in-selenium.html#schema-1114754","isPartOf":{"@id":"https://www.guru99.com/click-on-image-in-selenium.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/click-on-image-in-selenium.html#webpage"}}]}
```
