[英]Using selenium in python to find and download file
我嘗試在 web 上搜索類似問題,但找不到我要訪問的網站的這個特定元素的答案。 類似的方法適用於其他元素,因此請嘗試獲得一些幫助。
我想使用 python https://www.sgx.com/derivatives/negotiated-large-trade中的 Selenium 下載位於“下載”按鈕下的文件
該按鈕似乎位於:
<span class="table-action-label" title="Download" data-i18n="[title]sgx-table.toolbar.action-download;[text]sgx-table.toolbar.action-download" data-i18n-options="{}">Download</span>
我的代碼是
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from fake_useragent import UserAgent
ua = UserAgent()
ua_rand = ua.random
print(ua_rand)
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ua_rand
browser = webdriver.PhantomJS("C://, desired_capabilities=dcap)
browser.get("https://www.sgx.com/derivatives/negotiated-large-trade")
browser.find_element_by_xpath("//span[@class='table-action-label']").click()
browser.quit()
但它似乎沒有找到元素
NoSuchElementException: {"errorMessage":"Unable to find element with xpath '//span[@class='table-action-label']'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Content-Length":"119","Content-Type":"application/json
我怎樣才能下載文件?
非常感謝
你試過用這個 xpath: /html/body/div[1]/main/div[1]/article/template-base/div/div/sgx-widgets-wrapper/widget-derivatives-nlt/section[1] /div[1]/sgx-table/sgx-table-toolbar/div[2]/span
要單擊帶有文本作為下載的元素,您需要:
為invisibility_of_element()
sgx-loader 引入WebDriverWait 。
為element_to_be_clickable()
引入WebDriverWait
您可以使用以下任一定位器策略:
使用LINK_TEXT
:
driver.get("https://www.sgx.com/derivatives/negotiated-large-trade") WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Download"))).click()
使用CSS_SELECTOR
:
driver.get("https://www.sgx.com/derivatives/negotiated-large-trade") WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.table-action-label[title='Download']"))).click()
使用XPATH
:
driver.get("https://www.sgx.com/derivatives/negotiated-large-trade") WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='table-action-label' and @title='Download'][text()='Download']"))).click()
注意:您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
您可以在以下位置找到一些關於NoSuchElementException的相關討論:
謝謝你們的幫助,到目前為止,使用谷歌瀏覽器的一個版本如下。 我仍在嘗試使用 phantomJs 讓它工作。
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import time
from fake_useragent import UserAgent
ua = UserAgent()
ua_rand = ua.random
driver2 = webdriver.Chrome(executable_path=r"C:\Users\chromedriver.exe")
driver2.get("https://www.sgx.com/derivatives/negotiated-large-trade")
WebDriverWait(driver2, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader")))
time.sleep(5)
print("Downloading...")
WebDriverWait(driver2, 60).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='table-action-label' and @title='Download'][text()='Download']"))).click()
time.sleep(3)
driver2.quit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.