How do you click on an image in python selenium?

trying to click a input type using selenium python, input type calls image file, and added css cursor: pointer on the image, unfortunately cant click the image or the input

Image

Code


CSS

input[type="image" i] 
{
    cursor: pointer;
}

how to click on the image "Next Step" ?

I tried, but shows error

driver.find_element_by_xpath['//input[@type="image"][@src="/images/btn_next.png"]'].click[]

asked Jun 20, 2019 at 9:50

2

Try Use WebdriverWait and element_to_be_clickable to click on the image.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait[driver,20].until[EC.element_to_be_clickable[[By.XPATH,'//input[@type="image"][@src="/images/btn_next.png"]']]].click[]

If above code unable to click on the element try use javaScript executor to click on the element.

driver.execute_script["arguments[0].click[];",driver.find_element_by_xpath['//input[@type="image"][@src="/images/btn_next.png"]']]

answered Jun 20, 2019 at 10:08

KunduKKunduK

29.7k4 gold badges13 silver badges36 bronze badges

0

You were close. To click[] on the element you need to club up the attributes within the xpath using and and you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector["input[src='/images/btn_next.png'][type='image']"].click[]
    
  • Using xpath:

    driver.find_element_by_xpath["//input[@src='/images/btn_next.png' and @type='image']"].click[]
    

But as you intend to invoke click[] on the element, ideally you need to induce WebDriverWait for the element_to_be_clickable[] as follows:

  • Using css_selector:

    WebDriverWait[driver, 20].until[EC.element_to_be_clickable[[By.CSS_SELECTOR, "input[src='/images/btn_next.png'][type='image']"]]].click[]
    
  • Using xpath:

    WebDriverWait[driver, 20].until[EC.element_to_be_clickable[[By.XPATH, "//input[@src='/images/btn_next.png' and @type='image']"]]].click[]
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

answered Jun 20, 2019 at 10:07

1

If you run chrome then probably moving physical cursor to image and click can help. There is python package that move physical cursor to web element, selenium-move-cursor.

answered Jul 7, 2019 at 13:13

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

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["//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.

CONTRIBUTOR

Gutha Vamsi Krishna

How do you handle images in Selenium?

Mastering XPath and CSS Selector for Selenium An image in an html document has tagname. Each image also has an attribute src which contains the source of image in the page. To fetch any attribute in Selenium, we have to use the getAttribute[] method.

How do I insert an image into Selenium?

Using Selenium:.
Using Selenium: ... .
WebElement upload_file = driver. ... .
upload_file. ... .
Using AutoIT: ... .
WebElement browser = d.findElement[By.xpath["//input[@id='pimCsvImport_csvFile']"]]; //Browse button..

Where is xpath image in Selenium?

findElement[By. xpath["//img[contains[@src,'web/L001/images/IMAGENAME. jpg']]"]];

Chủ Đề