[英]Selenium opens only Google Chrome (using webdriver-manager)
[英]Error using webdriver-manager with pyinstaller
我的 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.4.2的版本,那么這個錯誤必須已經修復。
如果您使用webdriver_manager <= 3.4.2的版本,則錯誤仍然存在。
這是關於項目 Github 的拉取請求討論,它解決了這個問題。
要自己修復錯誤,我們需要在webdriver_manager/utils.py
中進行更改 - 導入附加模塊並更改兩個函數:
一開始導入subprocess
模塊
import subprocess
請在def chrome_version()
中找到代碼段:
with os.popen(cmd) as stream: stdout = stream.read()
並使用以下內容進行更改:
with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL, shell=True) as stream: stdout = stream.communicate()[0].decode()
對def firefox_version()
重復上述步驟。
強烈建議僅在您確定可以還原更改的情況下執行上述所有操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.