undetected-chromedriver icon indicating copy to clipboard operation
undetected-chromedriver copied to clipboard

Chromedriver console open with Pyinstaller

Open mrsachou opened this issue 2 years ago • 3 comments

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

mrsachou avatar May 06 '22 12:05 mrsachou

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.g service_creationflags=None here.
  • Then create the service and set the creationflags before initializing uc.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.

sebdelsol avatar May 06 '22 14:05 sebdelsol

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

ThomasTrovon avatar Jun 12 '22 16:06 ThomasTrovon

I have added this feature in my fork too. pip install git+https://github.com/sebdelsol/undetected-chromedriver@master

sebdelsol avatar Sep 02 '22 10:09 sebdelsol

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.

sebdelsol avatar Nov 06 '22 18:11 sebdelsol

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.

d0p0jake avatar Nov 09 '22 18:11 d0p0jake

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.

sebdelsol avatar Nov 10 '22 00:11 sebdelsol