1

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()
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Akshay Damle
  • 1,220
  • 3
  • 18
  • 31

4 Answers4

4

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.

Chanticleer
  • 461
  • 6
  • 12
2

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).

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • 1
    Tried that; it closes the browser too! :/ – Akshay Damle Sep 16 '14 at 14:50
  • 1
    @AkshayDamle hm, you want the browser to stay opened but close the chrome driver executeable service? – alecxe Sep 16 '14 at 14:51
  • 1
    Yes that's what I want to achieve – Akshay Damle Sep 16 '14 at 14:54
  • 1
    Thanks, this works! But now what if I want to use Chromedriver again later on [if I'm writing a dynamic program], I will have to create a webdriver again, use it, and then quit it. So the console window will pop open and close. But I don't want this, I only want to hide the console instead of stopping the service altogether. How do I accomplish this? – Akshay Damle Sep 16 '14 at 15:04
  • 1
    @AkshayDamle I see, then I guess passing `startupinfo` to the `subprocess.Popen` (as [suggested here](http://stackoverflow.com/a/1016651/771848)) should solve the issue. But, since the service startup is hidden inside the [`Service` class implementation](https://code.google.com/p/selenium/source/browse/py/selenium/webdriver/chrome/service.py#60), you would need to monkey-patch it. – alecxe Sep 16 '14 at 15:13
0

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")
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
0

This worked for me! This works if you need a completely windowless program (For example: if you need a sample of cookies). So no console output and no browser window

chrome_options.add_argument('--no-startup-window')

Here is a sample of how you would do it:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('headless')
chrome_options.add_argument('--no-startup-window')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://stackoverflow.com")

print(driver.get_cookies())

And also uh.. Make the python file a .pyw one instead of a .py to make its console window headless too (just rename it!)