我有一个访问网站和导出一些Excel文件的功能。为此,我从硒进口:
from selenium.webdriver.support.ui import WebDriverWait
我在WebDriverWait的'till'中调用函数every_downloads_chrome。
nameFile = WebDriverWait(driver, 120, 1).until(every_downloads_chrome)
# method to get the downloaded file name
def every_downloads_chrome(driver):
if not driver.current_url.startswith("chrome://downloads"):
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get("chrome://downloads/")
return driver.execute_script("""
var items = downloads.Manager.get().items_;
if (items.every(e => e.state === "COMPLETE"))
return items.map(e => e.fileUrl || e.file_url);
""")
这适用于我的电脑,一台Windows 10-谷歌Chrome版本79.0.3945.130(64位(。然而,当我在"生产电脑"上尝试时,Windows 10-谷歌80.0.3987.122(64位(,我有错误:
Erro Message: javascript error: Cannot read property 'get' of undefined
(Session info: chrome=80.0.3987.122)
Traceback (most recent call last):
File "<ipython-input-1-e86f4018e9d8>", line 394, in job
nameFile = WebDriverWait(driver, 120, 1).until(every_downloads_chrome)
File "C:Users***AppDataLocalContinuumanaconda3libsite-packagesseleniumwebdriversupportwait.py", line 71, in until
value = method(self._driver)
File "<ipython-input-1-e86f4018e9d8>", line 91, in every_downloads_chrome
""")
File "C:Users***AppDataLocalContinuumanaconda3libsite-packagesseleniumwebdriverremotewebdriver.py", line 636, in execute_script
'args': converted_args})['value']
File "C:Users***AppDataLocalContinuumanaconda3libsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:Users***AppDataLocalContinuumanaconda3libsite-packagesseleniumwebdriverremoteerrorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'get' of undefined
(Session info: chrome=80.0.3987.122)
问题是:下载。Manager.get((在Chrome版本80之后不起作用。
所以,我改变了#方法来获得下载的文件名
def every_downloads_chrome(driver):
if not driver.current_url.startswith("chrome://downloads"):
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get("chrome://downloads/")
return driver.execute_script("""
var elements = document
.querySelector('downloads-manager')
.shadowRoot.querySelector('#downloadsList').items;
if (elements.every(e => e.state === "COMPLETE"))
return elements.map(elements =>elements.fileUrl || elements.file_url);
""")