1

I am trying to download the csv file from this link with selenium: https://ourworldindata.org/grapher/annual-co-emissions-by-region and am encountering issues regarding finding the actual elements necessary to download the file. Here is my code so far:

driver.get("https://ourworldindata.org/grapher/annual-co-emissions-by-region")
    frame = driver.find_element_by_id("GTM-N2D4V8S")

    driver.switch_to.frame(frame)

    downloadLink = driver.find_element_by_xpath("/html/body/main/figure/div/div[4]/div/nav/ul/li[4]/a")
    driver.execute_script("arguments[0].click();", downloadLink)

Specifically, I am trying to find the second div underneath the element with this css selector body > main > figure > div > div.DownloadTab, which contains the download link. Unfortunatley, while I am able to find this parent element, I am unable to find the div I am looking for. From doing research, I think that this issue might have to do with the iframe, so I am trying to switch it with the code above (although it is not working). This link may be helpful: Selenium can't find elements even if they exist

Thanks so much!

Alex88
  • 51
  • 1
  • 6

1 Answers1

1

You should wait for the page / elements fully loaded before accessing them and to use the correct locators.
Try this:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)
driver.get("https://ourworldindata.org/grapher/annual-co-emissions-by-region")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li.download-tab-button"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[data-track-note='chart-download-csv']"))).click()

Prophet
  • 32,350
  • 22
  • 54
  • 79