Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

image
selenium
python

How to click on an image in Selenium Webdriver Python

Gutha Vamsi Krishna

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 webdriver
import time

#specify where your chrome driver present in your pc
PATH=r"C:\Users\gutkrish\Documents\chromedriver\chromedriver.exe"

#get instance of web driver
driver = webdriver.Chrome(PATH)

#provide website url here
driver.get("http://demo.guru99.com/test/newtours/")

#locate image element and click it
driver.find_element("tag name","img").click()

Explanation

  • Line 1: We import the webdriver from the selenium package.
  • 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.exe in 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.

RELATED TAGS

image
selenium
python
RELATED COURSES

View all Courses

Keep Exploring