All Yearsボタンをクリックした後、このWebサイトからcsvファイルをダウンロードしようとしています。
https://www.macrotrends.net/1476/copper-prices-historical-chart-data
ご覧のとおり、[すべての年]ボタンをクリックするxpathは/ html / body / div [1] / div [1] / div [3] / a [7]です。
これはAllYearsボタンの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()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加