Akshay Damle Akshay Damle - 2 months ago 54
Python Question

How to hide Chromedriver console window?

I have a simple Python script which uses selenium and webdriver to open up Facebook in a Chrome window and log in automatically.
When I run it, the Chromedriver console window opens up and stays open even after the entire program has finished execution, until I close it myself.

Is there a way to hide this console window? I have tried keeping a ".pyw" extension for my script, but that doesn't help since it's not the script's console window but the Chromedriver subprocess' console window that I wish to hide.

I couldn't find any resources on this. I think I might need to modify the chrome webdriver source code, but I don't know how. This is my code:

from selenium import webdriver
import sys

driver = webdriver.Chrome("C:\Python27\Scripts\chromedriver.exe")

driver.get("https://www.facebook.com")

email = driver.find_element_by_id("email")
passwd = driver.find_element_by_id("pass")

email.clear()
passwd.clear()

email.send_keys("example@example.com")
passwd.send_keys("examplepassword")

passwd.submit()

Answer

You need to call driver.quit() at the end of the script:

quit()

Closes the browser and shuts down the ChromeDriver executable that is started when starting the ChromeDriver

If you want to just close the service executable and let the browser stay opened, call:

driver.service.stop()

FYI, I've figured this out from the quit() method implementation (source code).