Issue
While trying to setup a headless browser using selenium a console window pops up briefly before it closes, it's not the chromedriver.exe window cause that's handled by CREATE_NO_WINDOW
the window is called selenium-manager.exe.
It only pops up when I build my script into an executable with Pyinstaller cause when I run it normally with Python it doesn't appear. The command I used was pyinstaller --windowed my_script.py
, I also tried adding --noconsole
and using --noconsole
alone but nothing worked. What could be causing this and how do I fix it?
Operation System: Windows-10-10.0.19045-SP0, Python: 3.11.1, Selenium: 4.10.0, Pyinstaller: 5.13.0
from subprocess import CREATE_NO_WINDOW
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import Chrome, ChromeOptions
def setup_options(options: ChromeOptions) -> ChromeOptions :
options.add_argument('--headless=new')
options.add_argument('--disable-extensions')
options.add_argument('--disable-infobars')
options.add_argument('--no-sandbox')
return options
def setup_driver() -> Chrome:
service_chrome = Service()
service_chrome.creation_flags = CREATE_NO_WINDOW
options = setup_options(ChromeOptions())
return Chrome(service=service_chrome, options=options)
driver = setup_driver()
# Do some automated work
driver.quit()
Here's a screenshot of the window:
Solution
Update
I made a merged pull request with the fix. This bug should only be present in selenium versions 4.10.0 and below.
Hacky fix, navigate to the following path in your virtual environment ".venv\lib\site-packages\selenium\webdriver\common\selenium_manager.py".
Within the script, there's a method called run
in the SeleniumManager
class that starts a subprocess
that executes the selenium-manager.exe they made an over-sight and assumed everyone would be running their scripts in a console environment or maybe didn't consider windows users, so when making the call to subprocess.run
they didn't add creationflags=CREATE_NO_WINDOW
.
Edit it as follows
if sys.platform == "win32":
completed_proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW)
else:
completed_proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Note that the bug is only present in windows. They'll probably address it in the next updates.
Answered By - Sen ZmaKi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.