In Selenium WebDriver, you can click on an image using different locators such as ID, Name, XPath, CSS Selector, etc. Since images (<img>
tags) do not have a click
event by default, we usually click them using their associated anchor (<a>
) tag or by directly clicking the image element.
Example 1: Clicking an Image Using ID Locator
If the image has a unique id
attribute, you can directly locate and click it.
HTML Example
<img id="logoImage" src="logo.png" alt="Company Logo">
Selenium Code
WebElement image = driver.findElement(By.id("logoImage"));
image.click(); // Clicks on the image
β
Best for: Images with unique id
attributes.
Example 2: Clicking an Image Inside a Link (<a>
Tag)
If the image is wrapped inside an <a>
tag, clicking the <img>
tag might not work. Instead, click the parent <a>
tag.
HTML Example
<a href="home.html">
<img src="home.png" alt="Home Button">
</a>
Selenium Code
WebElement imageLink = driver.findElement(By.xpath("//a/img"));
imageLink.click(); // Clicks on the image inside <a>
π Tip: Using //a/img
selects the <img>
inside <a>
, but if clicking the image doesnβt work, use //a
.
Example 3: Clicking an Image Using XPath Locator
If the image does not have an id
or name
, use XPath based on src
or alt
attributes.
WebElement image = driver.findElement(By.xpath("//img[@alt='Company Logo']"));
image.click();
Or by src
attribute:
WebElement image = driver.findElement(By.xpath("//img[contains(@src,'logo.png')]"));
image.click();
β
Best for: Images without id
but with unique alt
or src
attributes.
Example 4: Clicking an Image Using CSS Selector
Using CSS Selector
based on src
or alt
attributes:
WebElement image = driver.findElement(By.cssSelector("img[alt='Company Logo']"));
image.click();
β Best for: Short and efficient selectors.
Example 5: Clicking an Image Using JavaScript Executor (If Click Fails)
If .click()
does not work due to hidden or overlaying elements, execute JavaScript.
WebElement image = driver.findElement(By.xpath("//img[@alt='Company Logo']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", image);
β
Best for: Clicking hidden elements or when .click()
does not work.
Example 6: Clicking an Image Inside an iFrame
If the image is inside an iframe
, switch to the iframe before clicking.
driver.switchTo().frame("iframeID"); // Switch to iframe
WebElement image = driver.findElement(By.id("logoImage"));
image.click();
driver.switchTo().defaultContent(); // Switch back to main page
β Best for: Clicking images inside iframes.
Example 7: Clicking a Dynamically Loaded Image
If the image is loaded dynamically, use WebDriverWait
.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement image = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//img[@alt='Company Logo']")));
image.click();
β Best for: Clicking images that load with a delay.
Summary
Method | Best Use Case |
---|---|
By.id() |
When image has a unique id |
By.xpath() |
When identifying via alt or src |
By.cssSelector() |
For short and fast selectors |
By.tagName("img") |
When selecting all images |
JavaScriptExecutor |
When .click() doesnβt work |
Switch to iframe |
When image is inside an iframe |
WebDriverWait |
When image loads dynamically |
Would you like examples of dragging/dropping images in Selenium? π