Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide chromeDriver console in python

I'm using chrome driver in Selenium to open chrome , log into a router, press some buttons ,upload configuration etc. all code is written in Python.

here is the part of the code to obtain the driver:

chrome_options = webdriver.ChromeOptions()
prefs = {"download.default_directory":  self.user_local}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.experimental_options.
driver = webdriver.Chrome("chromedriver.exe", chrome_options=chrome_options)
driver.set_window_position(0, 0)
driver.set_window_size(0, 0)

return driver

when i fire up my app, i get a chromedriver.exe console (a black window) followed by a chrome window opened and all my requests are done.

My question: is there a way in python to hide the console window ?

(as you can see i'm also re-sizing the chrome window ,my preference would be doing things in a way the user wont notice anything happening on screen)

thanks Sivan

like image 746
sivan Avatar asked Nov 29 '15 14:11

sivan


People also ask

How do I hide the console window in Python?

Simply save it with a . pyw extension. This will prevent the console window from opening.

How do I disable ChromeDriver?

browser. close() will close only the current chrome window. browser. quit() should close all of the open windows, then exit webdriver.

Where is ChromeDriver in Selenium Python?

Now we need to move ChromeDriver somewhere that Python and Selenium will be able to find it (a.k.a. in your PATH ). The easiest place to put it is in C:\Windows . So move it there!

How do I hide a window in Selenium?

We can hide the Firefox window in Selenium webdriver. This can be done by making the browser headless. We shall achieve this with the FirefoxOptions class. We shall then create an object option of that class.


1 Answers

You will have to edit Selenium Source code to achieve this. I am a noob too, and I dont fully understand the overall consequences of editing source code but here is what I did to achieve hiding the webdriver console window on Windows 7, Python 2.7.

Locate and edit this file as follows: located at Lib\site-packages\selenium\webdriver\common\service.py in your Python folder.

Edit the Start() function by adding the creation flags this way: creationflags=CREATE_NO_WINDOW

The edited method will be as below:

def start(self):
    """
    Starts the Service.

    :Exceptions:
     - WebDriverException : Raised either when it can't start the service
       or when it can't connect to the service
    """
    try:
        cmd = [self.path]
        cmd.extend(self.command_line_args())
        self.process = subprocess.Popen(cmd, env=self.env,
                                        close_fds=platform.system() != 'Windows',
                                        stdout=self.log_file, stderr=self.log_file, creationflags=CREATE_NO_WINDOW)
    except TypeError:
        raise

You will have to add the relevant imports:

from win32process import CREATE_NO_WINDOW

This should also work for Chrome webdriver as they import the same file to start the webdriver process.

like image 126
Chanticleer Avatar answered Sep 17 '22 15:09

Chanticleer