无法让硒(python)下载没有链接但仅在我单击下载按钮后出现的csv文件



我正在尝试从本网站下载csv文件,然后单击"所有年份"按钮。

https://www.macrotrends.net/1476/copper-prices-historical-chart-data

如您所见,单击"所有年份"按钮的 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="数据下载"]

这是"下载历史数据"按钮的 HTML 代码

<button id="dataDownload" class="chart_buttons btn btn-danger btn-xs"><span class="glyphicon glyphicon-cloud-download"></span>&nbsp;&nbsp;<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中使用硒下载这个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)

该文件现在应该可以下载。

最新更新