How to click on an image in Selenium Webdriver Python
Overview
Selenium is an open-source web-based automation tool. In this answer, we will learn how to click on an image using Selenium Webdriver in Python.
First, we will find the image present in the DOM and click the image using the click() method.
Syntax
image_element.click()
Parameters
It doesn't take any parameters and won't return anything.
Example
from selenium import webdriverimport time#specify where your chrome driver present in your pcPATH=r"C:\Users\gutkrish\Documents\chromedriver\chromedriver.exe"#get instance of web driverdriver = webdriver.Chrome(PATH)#provide website url heredriver.get("http://demo.guru99.com/test/newtours/")#locate image element and click itdriver.find_element("tag name","img").click()
Explanation
- Line 1: We import the
webdriverfrom theseleniumpackage. - Line 2: We import the
time. - Line 5: We provide the path where we placed the driver of the web browser. For chrome, it is
chromedriver.exein the windows environment. - Line 8: We get the instance of the
webdriver. - Line 11: We provide the URL to the
driver.get()method to open it. - Line 14: We locate the image element using the tag name and click on it by calling the
click()method on that element.