【问题标题】:Error using webdriver-manager with pyinstaller将 webdriver-manager 与 pyinstaller 一起使用时出错
【发布时间】:2021-04-09 03:42:52
【问题描述】:

我的 webdriver-manager 运行良好,但是当我使用 pyinstaller 创建 .exe 文件时,出现以下错误。我发现如果我不将 --noconsole 放到 pyinstaller 命令中,它会起作用,但是使用 --noconsole 程序不起作用。 这是我的代码:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.google.com/")
driver.quit()

这是我使用 pyinstaller 创建 .exe 文件的方式:

pyinstaller --onefile --noconsole script.py

这是我得到的错误:

Traceback (most recent call last):
  File "script.py", line 797, in program2
  File "webdriver_manager\chrome.py", line 23, in __init__
  File "webdriver_manager\driver.py", line 54, in __init__
  File "webdriver_manager\utils.py", line 139, in chrome_version
  File "os.py", line 983, in popen
  File "subprocess.py", line 804, in __init__
  File "subprocess.py", line 1142, in _get_handles
OSError: [WinError 6] The handle is invalid

感谢您的帮助!

【问题讨论】:

  • 应该在昨天发布的 webdriver-manager 3.5.0 中修复

标签: python selenium webdriver pyinstaller webdriver-manager


【解决方案1】:

如果您使用webdriver_manager > 3.4.2的版本,那么这个bug必须已经修复了。

如果您使用webdriver_manager 的版本,那么错误仍然存​​在。

Here 是项目 Github 上的 pull request 讨论,解决了这个问题。

要自己修复错误,我们需要在webdriver_manager/utils.py中进行更改-导入附加模块并更改两个函数:

  1. 在开头导入subprocess模块

     import subprocess
    
  2. 请在def chrome_version()中找到sn-p:

     with os.popen(cmd) as stream: 
         stdout = stream.read()
    
  3. 并使用以下内容进行更改:

     with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL,
                           shell=True) as stream:
         stdout = stream.communicate()[0].decode()
    
  4. def firefox_version()重复上述步骤。

强烈建议仅在您确定可以还原更改的情况下执行上述所有操作。

【讨论】:

  • 这只是部分地为我工作。原来的控制台被隐藏了,尽管它最终打开了另一个显示不同日志的控制台?我尝试了这个组合,并让控制台在使用子进程 5 秒后消失。 Popen(cmd,shell = False,creationflags = 0x00000008,stdout = subprocess.PIPE,stderr = subprocess.DEVNULL,stdin = subprocess.DEVNULL)作为流:stdout = stream.communicate()[0].decode()版本= re .search(模式,标准输出)
猜你喜欢
  • 2020-12-20
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 2019-08-06
  • 2013-02-01
  • 2015-09-17
相关资源
最近更新 更多