martinkub
martinkub

Reputation: 41

Error using webdriver-manager with pyinstaller

My webdriver-manager worked perfectly but when I made .exe file with pyinstaller I got error below. I find out that if I won't put --noconsole to pyinstaller command it will work but with --noconsole the program isn't working. Here is my code:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

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

Here is how I created .exe file with pyinstaller:

pyinstaller --onefile --noconsole script.py

Here is the error which I got:

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

Thanks for help!

Upvotes: 2

Views: 1327

Answers (1)

Oleh Bedrii
Oleh Bedrii

Reputation: 21

If you use the version of webdriver_manager > 3.4.2, then this bug must to be fixed already.

If you use the version of webdriver_manager <= 3.4.2, then the error still persists.

Here is the pull request discussion on the project's Github, which fixes the issue.

To fix the error by ourselves, we need to make changes in webdriver_manager/utils.py - import additional module and change two functions:

  1. Import subprocess module in the beginning

     import subprocess
    
  2. Please find the snippet in def chrome_version():

     with os.popen(cmd) as stream: 
         stdout = stream.read()
    
  3. And change it with the following:

     with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, stdin=subprocess.DEVNULL,
                           shell=True) as stream:
         stdout = stream.communicate()[0].decode()
    
  4. Repeat step above for def firefox_version().

Highly recommend to do everything above only in case you are sure you can revert changes.

Upvotes: 2

Related Questions