How to hide Chromedriver console window?

前提是你 提交于 2019-12-29 08:18:16

问题


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

回答1:


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




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/25871898/how-to-hide-chromedriver-console-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!