0
votes

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>&nbsp;&nbsp;<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

2
<after I click the max button> - I can't see any max button in the URL you mentioned.David Piao
im so sorry my mistake i meant All Years, ive changed my question to reflect thatanarchy
i tried adding a Wait = WebDriverWait(driver,5).until(EC.presence_of_all_elements_located((By.XPATH,'/html/body/div[1]/div[1]/div[3]/a[7]'))) but it just says TimeoutExceptionanarchy
Don't change the question after you get answers. If you have a new question, then ask a new question. How are people supposed to give a new answer when you've already selected one?JeffC
I didn’t change the question, my question was originally 2 parts.anarchy

2 Answers

1
votes

I see that the elements you are trying to click are in an iframe element. You have to switch to the iframe first before clicking.

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()
1
votes

I encountered the same issue when I tried to download the csv file from macrotrends.net.

It seems that the site protects its download "button" from .click(), but I found I can interact it using selenium's keys.

So, at the beginning

from selenium.webdriver.common.keys import Keys

then, instead of using .click(),

driver.find_element_by_xpath(xpath).click()

using the following,

driver.find_element_by_xpath(xpath).send_keys(Keys.ENTER)

The file should be able to be downloaded now.