TypeError:通过Selenium调用Chrome Download Manager的execute_script()时,"NoneType"对象无法下标 [英] TypeError: 'NoneType' object is not subscriptable while invoking execute_script() for Chrome Download Manager through Selenium
问题描述
我一直在使用此功能 https://stackoverflow.com/a/48267887/11220889 等待下载完成并在完成后返回文件路径.到目前为止,它一直运转良好.
I have been using this function https://stackoverflow.com/a/48267887/11220889 for waiting for downloads to finish and returning the file path once finished. And it has been working great until now.
功能
def every_downloads_chrome(driver):
if not driver.current_url.startswith("chrome://downloads"):
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.file_url);
''')
它怎么称呼
paths = WebDriverWait(driver, 120, 1).until(every_downloads_chrome)
我收到错误
TypeError: 'NoneType' object is not subscriptable
因此,从我可以收集到的信息来看,某些变化已经开始,导致该函数无法返回路径.我相信这是由于chrome或更确切地说是chrome驱动程序发生了变化.我做出此假设的两个原因是:
So from what I can gather something has changed that has started causing the function to not return a path. I believe this is due to a change within chrome or even more specifically the chrome driver. My two reasons for this assumption is:
1)我在同事正在使用的另一个代码中具有此功能,她打电话给我,说昨天它产生了此错误.
1) I had this function in another code that my colleague was using and she called me saying it was producing this error yesterday.
2)两个代码都没有更改,因此更改必须是chrome
2)Neither code has changed so the change must be in chrome
我想继续使用此脚本,但是如果不可能的话,还有另一个函数等待下载完成并返回路径,理想情况下是通过驱动程序而不是通过文件路径来完成所有操作,因为多个用户在多台计算机上使用我的代码
I would like to keep using this script but if not possible have another function that waits for the downloads to finish and returns the paths and ideally does it all through the driver not through file path since multiple users use my codes on multiple machines.
版本-
Name: selenium
Version: 3.141.0
Name: Chrome Browser
Version: 73.0.3683.86
Name: Chrome Driver
Version: 2.43.600210
Name: System
Version: Windows 10 Pro x64
推荐答案
根据错误消息:
TypeError: 'NoneType' object is not subscriptable
您的主要问题似乎是所使用的二进制版本之间的不兼容性:
Your main issue seems to be incompatibility between the version of the binaries you are using as follows:
- 您正在使用 chromedriver = 2.43
- chromedriver = 2.43 的发行说明明确提到以下内容:
支持 Chrome v69-71
- 您正在使用 chrome = 73.0
- ChromeDriver v2.46 明确提及以下内容:
- You are using chrome=73.0
- Release Notes of ChromeDriver v2.46 clearly mentions the following :
- 将 ChromeDriver 升级到当前的 ChromeDriver v2.46 级.
- 保持 Chrome 版本为 Chrome v73 级别. (根据ChromeDriver v2.46发行说明) 通过 IDE
- 清理您的项目工作区和重建您的项目,并且仅具有必需的依赖项.
- 如果您的基本 Web客户端版本太旧,则将其卸载并安装最新版本的 Web客户端 GA.
- 执行您的
@Test
. - 始终在
tearDown(){}
方法中调用driver.quit()
以关闭&优雅地销毁 WebDriver 和 Web Client 实例. - Upgrade ChromeDriver to current ChromeDriver v2.46 level.
- Keep Chrome version at Chrome v73 level. (as per ChromeDriver v2.46 release notes)
- Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
- If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
- Execute your
@Test
. - Always invoke
driver.quit()
withintearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully. - 获取来自渲染器的超时消息:600.000当我们使用Jenkins Windows服务模式执行Selenium脚本时
- 已定时接收来自渲染器的消息:10.000,同时在Windows上通过Jenkins使用chromedriver和chrome捕获屏幕截图
- Getting Timed out receiving message from renderer: 600.000 When we execute selenium scripts using Jenkins windows service mode
- Timed out receiving message from renderer: 10.000 while capturing screenshot using chromedriver and chrome through Jenkins on Windows
支持 Chrome v71-73
因此 ChromeDriver v2.43 与 Chrome浏览器v73.0
当前GAed Chrome v73 出现了一些问题,您可能希望降级为 Chrome v72 .您可以在以下位置找到一些相关的讨论:
Currently GAed Chrome v73 have some issues and you may like to downgrade to Chrome v72. You can find a couple of relevant discussions in:
这篇关于TypeError:通过Selenium调用Chrome Download Manager的execute_script()时,"NoneType"对象无法下标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!