[英]How to click on the image selenium python
你近了 要在元素上click()
,您需要使用和組合 xpath中的屬性 ,並且您可以使用以下定位策略之一 :
使用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
嘗試使用WebdriverWait
和element_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"]'))
如果您運行chrome,則可能將物理光標移動到圖像上並單擊會有所幫助。 有一個python包可以將物理光標移動到Web元素selenium-move-cursor。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.