如何单击图像硒python

时间:2019-06-20 09:50:15

标签: python selenium selenium-webdriver xpath css-selectors

尝试使用硒python单击输入类型,输入类型调用图像文件,并在图像上添加了CSS光标:指针,不幸的是无法单击图像或输入

图片

enter image description here

代码

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

CSS

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

如何单击图像“下一步”?

我尝试过,但显示错误

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

3 个答案:

答案 0 :(得分:0)

你很近。要在元素上click(),您需要使用属性组合在 xpath 中,并且可以使用以下任意一种Locator Strategies

  • 使用css_selector

    driver.find_element_by_css_selector("input[src='/images/btn_next.png'][type='image']").click()
    
  • 使用xpath

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

但是,当您打算在元素上调用click()时,理想情况下,您需要为element_to_be_clickable()引入 WebDriverWait ,如下所示:

  • 使用css_selector

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

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@src='/images/btn_next.png' and @type='image']"))).click()
    
  • 注意:您必须添加以下导入:

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

答案 1 :(得分:0)

尝试使用WebdriverWaitelement_to_be_clickable单击图像。

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()

如果以上代码无法单击该元素,请尝试使用javaScript executor单击该元素。

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

答案 2 :(得分:0)

如果您运行chrome,则可能将物理光标移至图像并单击会有所帮助。有一个python软件包可以将物理光标移动到Web元素selenium-move-cursor。