How to click on image in selenium webdriver Python?

We can click on an image with Selenium webdriver in Python using the method click. First of all, we have to identify the image with the help of any of the locators like id, class, name, css, xpath, and so on. An image in the html code is represented by the img tagname.

Let us see the html code of an image element.


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class ImageClk{
   public static void main(String[] args) {
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      // identify image
      WebElement l = driver.findElement(By.xpath("//img[@alt='Tutorialspoint']"));
      String s = l.getAttribute("title");
      System.out.println("Title attribute value is :" + s);;


Updated on: 03-Apr-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started