0

I am trying to download a file from the following link by clicking the download button: https://www.investing.com/equities/oil---gas-dev-historical-data

Here is my code:

from datetime import date
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementClickInterceptedException
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.common.exceptions import TimeoutException

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.preferences.instantApply",True)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
profile.set_preference("browser.helperApps.alwaysAsk.force",False)
profile.set_preference("browser.download.manager.showWhenStarting",False)
profile.set_preference("browser.download.folderList",0)


driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://www.investing.com/equities/oil---gas-dev-historical-data')

try:
    popup = WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "i[class*='largeBannerCloser']")))
    popup.click()
except TimeoutException as to:
    print(to)

#driver.find_element_by_css_selector("i.popupCloseIcon").click()
driver.find_element_by_css_selector("a[class*='login']").click()
driver.find_element_by_id('loginFormUser_email').send_keys('myemail')
driver.find_element_by_id('loginForm_password').send_keys('pass')
driver.find_element_by_xpath("//div[@id='loginEmailSigning']//following-sibling::a[@class='newButton orange']").click()

driver.find_element_by_id('flatDatePickerCanvasHol').click()
start_date = driver.find_element_by_id('startDate')
start_date.send_keys(Keys.BACKSPACE*10)
start_date.send_keys(date(2014,1,1).strftime("%d/%m/%Y"))
driver.find_element_by_id('applyBtn').click()
#driver.implicitly_wait(30)
driver.find_element_by_css_selector('a.newBtn.LightGray.downloadBlueIcon.js-download-data').click()

But it always saves empty file? How can I avoid this behavior?

1 Answer 1

1

The page seems to export data displayed on the page only. adding a check to just wait for the table to load.

After changing the date range helped in downloading the data successfully.

try:
    popup = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "//div[@id='results_box']//tbody//tr")))
except TimeoutException as to:
    print(to)
driver.find_element_by_css_selector('a.newBtn.LightGray.downloadBlueIcon.js-download-data').click()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.