How do I hide the Chromedriver console window?
I have a simple Python script that uses selenium and webdriver to open Facebook in a Chrome window and auto login. When I run it, the Chromedriver console window opens and stays open even after the entire program finishes, until I close it myself.
Is there a way to hide this console window? I tried to keep the ".pyw" extension for my script, but that doesn't help as it is not the script console window, but the Chromedriver subprocessor console window that I want 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()
source to share
You need to call driver.quit()
at the end of your script:
quit()
Closes the browser and disables the ChromeDriver executable that starts when the ChromeDriver starts
If you just want to close the service executable and leave the browser open, call:
driver.service.stop()
FYI, I figured it out from the method implementation quit()
( source code ).
source to share
I had the same problem, but when I run driver.service.stop()
it closes Chrome. I worked around it by importing os and firing the task into the chrome process.
This is another option: first change the script extension from .py
to .pyw
, then:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
driver = webdriver.Chrome(executable_path='C:/apps/chromedriver.exe', service_args=["--verbose", '--log-path=c:/logs/logs/qc1.log'])
driver.get("https://example.com")
switch = driver.find_element_by_id("signInSbmtBtn")
password = driver.find_element_by_id("password")
username = driver.find_element_by_id("userid")
username.send_keys('user');
password.send_keys('password');
switch.click();
os.system("taskkill /im chromedriver.exe")
source to share
To hide the webdriver console window I had to edit Lib \ site-packages \ selenium \ webdriver \ common \ services.py in my case, but I was using PhantomJS. PhantomJS will import and use this file to start its process. Basically, I added the following create flag to the Start method:
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` in bold.
Also add this line to import from win32process import CREATE_NO_WINDOW
This should also work for the Chrome web browser, as its service.py also imports this same file, although I haven't had time to try it.
source to share