問題描述
我正在從這個網站上抓取數據.元素在下面和geckodriver
I am scraping data from this website . The element is below and geckodriver
<img class="getdata-button" style="float:right;" src="/common/images/btn-get-data.gif" id="get" onclick="document.getElementById('submitMe').click()">
但無法讓 selenium 單擊它甚至嘗試了 xpath、id 但不是運氣是否有任何修復或解決方法來完成它?
but can't get selenium to click it tried even xpath, id but not luck is there any fix or work around to get it done?
推薦答案
要點擊元素 Get Data,您可以使用以下任一 定位器策略:
To click on the element Get Data you can use either of the following Locator Strategies:
使用
css_selector
:
driver.find_element_by_css_selector("img.getdata-button#get").click()
使用 xpath
:
driver.find_element_by_xpath("//img[@class='getdata-button' and @id='get']").click()
理想情況下,點擊你需要誘導的元素 WebDriverWait 用于 element_to_be_clickable()
并且您可以使用以下任一 定位器策略:
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
使用
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img.getdata-button#get"))).click()
使用 XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[@class='getdata-button' and @id='get']"))).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
這篇關于Selenium 在使用 Python 時無法單擊“獲取數據"按鈕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!