1

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

enter image description here

Code

<input type="image" src="/images/btn_next.png">

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()
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Harry Edward
  • 91
  • 2
  • 11

3 Answers3

1

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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    Thanks for the detailed answer, its working perfectly. :) – Harry Edward Jun 20 '19 at 10:27
  • @HarryEdward Glad to be able to help you. Please [_accept_](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) the _answer_ by clicking on the hollow tick mark beside my _answer_ which is just below the _votedown_ arrow, so the tick mark turns _green_. – DebanjanB Jun 20 '19 at 10:28
1

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"]'))
KunduK
  • 26,790
  • 2
  • 10
  • 32
0

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.

smirad91
  • 1
  • 1