Python 如何点击图片

Python 如何点击图片,python,selenium,selenium-webdriver,xpath,css-selectors,Python,Selenium,Selenium Webdriver,Xpath,Css Selectors,尝试使用selenium python单击输入类型时,输入类型调用图像文件,并在图像上添加了css游标:指针,不幸的是,无法单击图像或输入 图像 <input type="image" src="/images/btn_next.png"> 代码 <input type="image" src="/images/btn_next.png"> 如何点击图片“下一步” 我试过了,但显示出错误 driver.find_element_by_xpath('//input[@

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

图像

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

代码

<input type="image" src="/images/btn_next.png">
如何点击图片“下一步”

我试过了,但显示出错误

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

你很接近。要
单击元素上的()
,您需要使用组合xpath中的属性,并且可以使用以下任一选项:

  • 使用
    css\u选择器

    driver.find_element_by_css_selector("input[src='/images/btn_next.png'][type='image']").click()
    
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((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()
    
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@src='/images/btn_next.png' and @type='image']"))).click()
    
但是,当您打算在元素上调用
click()
时,理想情况下需要引导WebDriverWait使
元素成为可点击的()
,如下所示:

  • 使用
    css\u选择器

    driver.find_element_by_css_selector("input[src='/images/btn_next.png'][type='image']").click()
    
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((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()
    
    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
    

尝试使用
WebdriverWait
元素可点击
点击图像

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"]'))

若你们运行chrome,那个么将物理光标移动到图像并点击可能会有所帮助。有一个python软件包可以将物理光标移动到web元素selenium move cursor。

出现了什么错误?感谢您的帮助,伙计,问题已解决:)谢谢您的详细回答,它工作得很好。:)@HarryEdward很高兴能帮助你。请点击“我的答案”旁边的空心记号(位于votedown箭头下方)来输入答案,这样记号就会变成绿色。