簡體   English   中英

如何使用硒單擊圖像

[英]How to click on image using selenium

我正在嘗試單擊包含此代碼的按鈕:

<div style="text-align:center" id="plus_res">
<img src="https://www.tunlancer.com/theme/images/plus_resv2.png" align="plus d'elements" title="plus d'elements" style="cursor:pointer" onclick="plus_resultat()">
</div>

我試過了 :

image = driver.find_element_by_xpath("//img[contains(@src,'https://www.tunlancer.com/theme/images/plus_resv2.png')]")

driver.execute_script("arguments[0].click();", image)
driver.find_element_by_id('plus_res').click()
driver.find_element_by_xpath('//*[@id="plus_res"]/img').click()

但它顯示了 NoSuchElementException 。 請問我該如何解決?

可能您必須添加延遲。
這樣的事情應該工作:

time.sleep(5)
driver.find_element_by_css_selector('img[src="https://www.tunlancer.com/theme/images/plus_resv2.png"]').click()

我猜您在訪問該元素之前錯過了延遲/等待。
請試試這個:

wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, '//img[@onclick="plus_resultat()"]'))).click()

您將需要以下導入:

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

也可能該元素在 iframe 內。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM