[英]Unable to get selenium (python) to download a csv file which doesnt have a link but only appears after i click the download button
單擊“所有年份”按鈕后,我正嘗試從此網站下載 csv 文件。
https://www.macrotrends.net/1476/copper-prices-historical-chart-data
如您所見,單擊 All Years 按鈕的 xpath 是 /html/body/div[1]/div[1]/div[3]/a[7]
這是所有年份按鈕的 html 代碼
<a class="zoom external-period-changer" data-period-label=" All ">All Years</a>
和單擊下載歷史數據按鈕的 xpath 是 //*[@id="dataDownload"]
這是下載歷史數據按鈕的 html 代碼
<button id="dataDownload" class="chart_buttons btn btn-danger btn-xs"><span class="glyphicon glyphicon-cloud-download"></span> <strong>Download Historical Data</strong></button>
這是我的代碼
import time
import requests
from bs4 import BeautifulSoup
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.firefox.options import Options
start_time = time.time()
options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--disable-extensions")
driver = webdriver.Firefox(executable_path=r"/home/geckodriver/geckodriver",options=options,)
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', '/home/Documents/testing/macrotrends')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')
commodity = '1476/copper-prices-historical-chart-data'
url = "https://www.macrotrends.net/"+ commodity
driver.get(url)
time.sleep(5)
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]/a[7]').click()
time.sleep(1.5)
driver.find_element_by_xpath('//*[@id="dataDownload"]').click()
time.sleep(1.5)
driver.close()
print("--- %s seconds ---" % (time.time() - start_time))
但我收到以下錯誤:
NoSuchElementException: Message: Unable to locate element: /html/body/div[1]/div[1]/div[3]/a[7]
首先,為什么我不能點擊這個按鈕? 當我可以清楚地檢查元素並看到它在那里時。
此外,通常大多數網站都會出現下載鏈接,我可以只使用請求來獲取 csv 文件。 但由於某種原因,鏈接沒有出現。
有什么方法或更好的方法可以在 python 中使用 selenium 下載這個 csv 文件?
編輯:
所以現在我根據答案添加了這個並將代碼更改為以下內容
start_time = time.time()
options = Options()
driver = webdriver.Firefox(executable_path=r"/home/geckodriver/geckodriver",options=options,)
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', '/home/Documents/testing/macrotrends')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')
driver.get('https://www.macrotrends.net/1476/copper-prices-historical-chart-data')
time.sleep(5)
iframe = driver.find_element_by_xpath("//iframe[@id='chart_iframe']")
driver.switch_to.frame(iframe)
xpath = "//a[text()='All Years']"
driver.find_element_by_xpath(xpath).click()
xpath = "//button[@id='dataDownload']"
driver.find_element_by_xpath(xpath).click()
time.sleep(10)
driver.close()
print("--- %s seconds ---" % (time.time() - start_time))
這次我能夠找到元素,但它在無頭模式下不起作用。 謝謝您的幫助
我看到您嘗試單擊的元素位於 iframe 元素中。 您必須先切換到 iframe,然后再單擊。
driver.get('https://www.macrotrends.net/1476/copper-prices-historical-chart-data')
iframe = driver.find_element_by_xpath("//iframe[@id='chart_iframe']")
driver.switch_to.frame(iframe)
xpath = "//a[text()='All Years']"
driver.find_element_by_xpath(xpath).click()
xpath = "//button[@id='dataDownload']"
driver.find_element_by_xpath(xpath).click()
當我嘗試從 macrotrends.net 下載 csv 文件時遇到了同樣的問題。
該站點似乎保護其下載“按鈕”免受.click()
,但我發現我可以使用 selenium 的密鑰對其進行交互。
所以,一開始
from selenium.webdriver.common.keys import Keys
然后,而不是使用.click()
,
driver.find_element_by_xpath(xpath).click()
使用以下,
driver.find_element_by_xpath(xpath).send_keys(Keys.ENTER)
該文件現在應該可以下載了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.