undetected-chromedriver
undetected-chromedriver copied to clipboard
Chromedriver console open with Pyinstaller
Hello, when i compile with PyInstaller the Chromedriver console open also, i know with selenium4 now we can hide the console like this :
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService # Similar thing for firefox also!
from subprocess import CREATE_NO_WINDOW # This flag will only be available in windows
# Define your own service object with the `CREATE_NO_WINDOW ` flag
# If chromedriver.exe is not in PATH, then use:
# ChromeService('/path/to/chromedriver')
chrome_service = ChromeService('chromedriver')
chrome_service.creationflags = CREATE_NO_WINDOW
driver = webdriver.Chrome(service=chrome_service)
But its not working with undetected chromedriver Thanks for your help
You're right you can't forward an already created service to uc.Chrome()
. What makes it a little harder is that you've to create the service with the actual executable_path
that points to the patched chromedriver and that's done in uc.Chrome.__init__()
.
So you'll have to actually modify uc.Chrome()
class to make it works. It's defined in this file here :
- First you'll need to import Selenium service :
from selenium.webdriver.chrome.service import Service
here. - And add a new kwarg to the
uc.Chrome()
class : e.gservice_creationflags=None
here. - Then create the service and set the
creationflags
before initializinguc.Chrome()
parent class here :
if service_creationflags:
service = Service(patcher.executable_path, port, service_args, service_log_path)
service.creationflags = service_creationflags
else:
service = None
- Finally forward the service argument to the
uc.Chrome()
parent class here :
super(Chrome, self).__init__(
executable_path=patcher.executable_path,
port=port,
options=options,
service_args=service_args,
desired_capabilities=desired_capabilities,
service_log_path=service_log_path,
keep_alive=keep_alive,
service=service, # needed or the service will be re-created
)
Usage:
from subprocess import CREATE_NO_WINDOW
import undetected_chromedriver as uc
if __name__ == "__main__":
driver = uc.Chrome(service_creationflags=CREATE_NO_WINDOW)
Here's the >>gist for it<<. Please be aware it's based on my own undetected-chromedriver fork in which I've added some evasions for headless mode among other things..
Feel free to create a pull request for this. I've tested it and it works, please tell me if you need more.
You're right you can't forward an already created service to . What makes it a little harder is that you've to create the service with the actual that points to the patched chromedriver and that's done in .
uc.Chrome()``executable_path``uc.Chrome.__init__()
So you'll have to actually modify class to make it works. It's defined in this file here :
uc.Chrome()
- First you'll need to import Selenium service : here.
from selenium.webdriver.chrome.service import Service
- And add a new kwarg to the class : e.g here.
uc.Chrome()``service_creationflags=None
- Then create the service and set the before initializing parent class here :
creationflags``uc.Chrome()
if service_creationflags: service = Service(patcher.executable_path, port, service_args, service_log_path) service.creationflags = service_creationflags else: service = None
- Finally forward the service argument to the parent class here :
uc.Chrome()
super(Chrome, self).__init__( executable_path=patcher.executable_path, port=port, options=options, service_args=service_args, desired_capabilities=desired_capabilities, service_log_path=service_log_path, keep_alive=keep_alive, service=service, # needed or the service will be re-created )
Usage:
from subprocess import CREATE_NO_WINDOW import undetected_chromedriver as uc if __name__ == "__main__": driver = uc.Chrome(service_creationflags=CREATE_NO_WINDOW)
Here's the >>gist for it<<. Please be aware it's based on my own undetected-chromedriver fork in which I've added some evasions for headless mode among other things..
Feel free to create a pull request for this. I've tested it and it works, please tell me if you need more.
Bro... YOU ARE THE MAN! Worked perfectly!
Pls @ultrafunkamsterdam add this in main version. s2 <3
I have added this feature in my fork too.
pip install git+https://github.com/sebdelsol/undetected-chromedriver@master
Note that Selenium has turned its Service.creationflags
into Service.creation_flags
since version 4.6.0.
So I changed the relevant code here in my fork. It works for any version of selenium.
Hi, when I compile to .exe its still showing the blank console together with chrome. I use auto-py-to-exe as my .exe converter. Here is my code
```
import undetected_chromedriver as uc
from subprocess import CREATE_NO_WINDOW
driver = uc.Chrome(service_creationflags=CREATE_NO_WINDOW, use_subprocess=True)
driver.get("https://www.samplesite.com/login")
```
If I remove use_subprocess=True
chrome won't launch at all.
service_creationflags
is not implemented on this version, so you can either wait for the relevant pull request to be merged or use my fork in the meantime.
use_subprocess
is already set to True
by default. If you set it to False
then you'll have to protect your main entry point and add freeze_support()
when you produce a Windows executable.
Please be aware that my fork don't set use_subprocess
to True
by default.