简体   繁体   English

Selenium Python - 下载 button.click() 不再工作

[英]Selenium Python - download button .click() no longer working

I have the following code which was working until recently - the aim is to download some historic data from the site which is shown in the code:我有以下代码,直到最近才有效 - 目的是从代码中显示的站点下载一些历史数据:

import os
import time
import pandas as pd
from datetime import datetime
from selenium import webdriver

# Set option variables for using ChromeDriver

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable_dev-shm-usage')

options.add_experimental_option("prefs", {
  "download.default_directory": r"\\filepath\foldernamefordownload",
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
  "safebrowsing.enabled": True
})

# Set up ChromeDriver and open path

wd = webdriver.Chrome(r'filepath\chromedriver.exe', options=options)

solar_path = 'https://www.solar.sheffield.ac.uk/pvlive/'
wd.get(solar_path)
time.sleep(10)

# Click on data download ribbon

data_dwnload1 = wd.find_element_by_id('dataDownloadPanelTitle')
data_dwnload1.click()
time.sleep(5)

# Download data

data_dwnload2 = wd.find_element_by_id('download-csv-button')
data_dwnload2.click()
time.sleep(30)

wd.close()
del solar_path

The problem is that this no longer works - nothing downloads.问题是这不再有效 - 没有下载。 What am I missing here?我在这里想念什么? The code still runs (there is code after this section which only breaks when it tries to look for the downloaded file which no longer exists).代码仍在运行(本节之后的代码仅在尝试查找不再存在的下载文件时才会中断)。

Any help would be much appreciated.任何帮助将非常感激。

Simplified a bit bu using explicit waits and it is working.使用显式等待简化了一点,它正在工作。 Please check.请检查。 The time.sleep(30) seems optional. time.sleep(30)似乎是可选的。 You can wait for a while and then close, but it's your choice if you want to wait for 30 sec.您可以等待一段时间然后关闭,但如果您想等待 30 秒,这是您的选择。

wd = webdriver.Chrome(r'filepath\chromedriver.exe', options=options)
solar_path = 'https://www.solar.sheffield.ac.uk/pvlive/'
wd.get(solar_path)
time.sleep(10)
# Click on data download ribbon
WebDriverWait(wd, 20).until(EC.visibility_of_element_located((By.ID, 'dataDownloadPanelTitle'))).click()
# Download data
WebDriverWait(wd, 20).until(EC.visibility_of_element_located((By.ID, 'download-csv-button'))).click()
time.sleep(30)

wd.close()

downloaded file snapshot: click here for file snapshot下载的文件快照:单击此处获取文件快照

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM