0

I am trying to make a background web-crawler in python. I have managed to write the code for it and then I used the pythonw.exe app to execute it without any console window. Also, I ran ChromeDriver in headless mode.

The problem is, it still produces a console window for the ChromeDriver which says $ DevTools listening on ...some address.

How can I get rid of this window?

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
Ony10
  • 3
  • 3
  • Look at [Can Selenium Webdriver open browser windows silently in background?](https://stackoverflow.com/questions/16180428/can-selenium-webdriver-open-browser-windows-silently-in-background) - seems helpful – Aviv Yaniv Aug 25 '20 at 09:25
  • You can silently run your chromedriver in background
    Check is the existing answer on it. [old-answer](https://stackoverflow.com/questions/16180428/can-selenium-webdriver-open-browser-windows-silently-in-background?answertab=oldest#tab-top)
    – Suryaveer Singh Aug 25 '20 at 09:33

2 Answers2

0

Even if you make the script as .pyw, when the new process chromedriver.exe is created, a console window appears for that. There is an option to turn on the option CREATE_NO_WINDOW in C#, but there is not one yet in Python bindings for selenium. I was planning to fork selenium and add this feature myself.

Solution for now (only for Windows): Edit the selenium library

Go to this folder: C:\Users\name\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\selenium\webdriver\common\ (The path till Python38-32 depends on your installation of python).

There will be a file named service.py, which you need to edit as follows:

  • Add the import statement at the top from subprocess import STDOUT, CREATE_NO_WINDOW
  • Now (maybe in the line numbers 72 to 76), you must add another option creationflags=CREATE_NO_WINDOW in the function subprocess.Popen(). To make it clear, see before and after versions of code below:

Before edit:

self.process = subprocess.Popen(cmd, env=self.env,
                                close_fds=platform.system() != 'Windows',
                                stdout=self.log_file,
                                stderr=self.log_file,
                                stdin=PIPE)

After edit:

self.process = subprocess.Popen(cmd, env=self.env,
                                close_fds=platform.system() != 'Windows',
                                stdout=self.log_file,
                                stderr=self.log_file,
                                stdin=PIPE
                                creationflags=CREATE_NO_WINDOW) # Here !

Note:

Make a copy of the old service.py file, so that you replace it back if you need to in the future.

Ali Sajjad
  • 3,589
  • 1
  • 28
  • 38
0

I modified selenium lib and was able to get rid of this window.

In my case, this is the modified file path:

C:\Python37-32\Lib\site-packages\selenium\webdriver\common\service.py

Please see modified section

Shymaxtic
  • 3
  • 2
  • 3