How to click on image in selenium webdriver Python?

SeleniumAutomation TestingTesting Tools

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.

Example

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) {
      System.setProperty("webdriver.gecko.driver",
"C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://www.tutorialspoint.com/index.htm");
      // identify image
      WebElement l = driver.findElement(By.xpath("//img[@alt='Tutorialspoint']"));
      String s = l.getAttribute("title");
      System.out.println("Title attribute value is :" + s);
      l.click();
      driver.quit();
   }
}

Output

raja
Published on 03-Apr-2021 14:20:59
Advertisements