Chapter 17_2: How to Click on Image in Selenium

Accessing Image Links

Image links are the links in web pages pictured by an image which when clicked navigates to a different window or page.Since they are images, we cannot use the By.linkText() and By.partialLinkText() methods because image links basically have no link texts at all.

In this case, we should resort to using either By.cssSelector or By.xpath. The first method is more preferred because of its simplicity.

In the example below, we will access the “ToolsQA” logo

We will use By.cssSelector and the element’s “title” attribute to access the image link. And then we will verify if we are taken to ToolsQA homepage.

package Testology;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class myclass {

	public static void main(String[] args) {
		System.setProperty("webdriver.driver.chromedriver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		String baseUrl = ("");

		// click on the "ToolsQA" logo