在 pyinstaller 中以窗口模式导出到 EXE 后,Selenium 不起作用

Ahm*_*aha 0 python selenium pyinstaller python-3.x

我正在制作一个需要使用硒的 PyQt4 应用程序。开发时一切正常,但是当我通过pyinstaller导出到单个文件EXE并且没有 console 时,它会产生以下回溯错误:

[WinError6] The handle is invalid
Run Code Online (Sandbox Code Playgroud)

当我在console = True(在 pyinstaller 规范文件中)导出它时不会发生这种情况,该错误仅在没有 console 的情况下产生。

产生的错误在以下行中:

driver = webdriver.Chrome(executable_path="chromedriver.exe")
Run Code Online (Sandbox Code Playgroud)

我的规格:

Python:3.4
架构:64 位
Selenium:3.6.0
Pyinstaller:3.3
操作系统:Windows 10

我用谷歌搜索了大约 1 小时,但找不到任何解决方案:(

Ahm*_*aha 8

经过大量研究,我找到了上述问题的解决方案。

您只需要编辑文件:
C:\Python34\Lib\site-packages\selenium\webdriver\common\service.py

更改以下行:

self.process = subprocess.Popen(cmd, env=self.env,
                                        close_fds=platform.system() != 'Windows',
                                        stdout=self.log_file, stderr=self.log_file)
Run Code Online (Sandbox Code Playgroud)

到:

self.process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False, creationflags=0x08000000)
Run Code Online (Sandbox Code Playgroud)

即使在开发过程中以及部署到 EXE 之后,这也将起作用。

可能是硒错误。