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 win
To hide the webdriver console window, I had to edit the Lib\site-packages\selenium\webdriver\common\services.py in my case but I was using PhantomJS. PhantomJS imports and uses this file to start its process. Basically, I added the following creation 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 to the imports this line from win32process import CREATE_NO_WINDOW
This should also work for the Chrome webdriver, as its service.py also imports this very same file, though I have not had time to try.
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).
I had the same problem, but when I run driver.service.stop()
it closes Chrome. I worked around it by importing os and firing off a task kill to the chrome process.
This is another option: first change 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")