I am trying to download a csv file from this website after I click the All Years button.
https://www.macrotrends.net/1476/copper-prices-historical-chart-data
as you can see the xpath to click the All Years button is /html/body/div[1]/div[1]/div[3]/a[7]
this is the html code of the All Years button
<a class="zoom external-period-changer" data-period-label=" All ">All Years</a>
and the xpath to click the Download Historical Data button is //*[@id="dataDownload"]
here is the html code of the Download Historical Data button
<button id="dataDownload" class="chart_buttons btn btn-danger btn-xs"><span class="glyphicon glyphicon-cloud-download"></span> <strong>Download Historical Data</strong></button>
Here is my code
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))
but i get the following error:
NoSuchElementException: Message: Unable to locate element: /html/body/div[1]/div[1]/div[3]/a[7]
First off, why cant i click this button? when i can clearly inspect the element and see that it's there.
Also, usually a download link appears from most websites and i can just use requests to get the csv file. But for some reason the link doesnt appear.
Is there any way or a better way to download this csv file using selenium in python?
Edited:
So now i have added this according to the answers and changed the code to the following
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))
I am able to find the elements this time, but it just doesnt work in headless mode. thanks for the help