python: Selenium сохраняет пустой файл - PullRequest
0 голосов
/ 18 марта 2020

Я пытаюсь загрузить файл по следующей ссылке, нажав кнопку загрузки: https://www.investing.com/equities/oil---gas-dev-historical-data

Вот мой код:

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

Но это всегда сохраняет пустой файл? Как я могу избежать этого поведения?

1 Ответ

1 голос
/ 18 марта 2020

Похоже, что страница экспортирует данные, отображаемые только на странице. добавив проверку, чтобы просто дождаться загрузки таблицы.

После изменения диапазона дат помогла успешная загрузка данных.

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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...