问题描述
我有一个简单的 Python 脚本,它使用 selenium 和 webdriver 在 Chrome 窗口中打开 Facebook 并自动登录.当我运行它时,Chromedriver 控制台窗口打开并保持打开状态,即使在整个程序完成执行后,直到我自己关闭它.
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.
有没有办法隐藏这个控制台窗口?我尝试为我的脚本保留一个.pyw"扩展名,但这并没有帮助,因为它不是脚本的控制台窗口,而是我希望隐藏的 Chromedriver 子进程的控制台窗口.
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.
我找不到这方面的任何资源.我想我可能需要修改 chrome webdriver 源代码,但我不知道如何.这是我的代码:
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:Python27Scriptschromedriver.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()
推荐答案
需要调用driver.quit() 在脚本末尾:
You need to call driver.quit() at the end of the script:
quit()
关闭浏览器并关闭 ChromeDriver 可执行文件即启动 ChromeDriver 时启动的
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()
仅供参考,我已经从 quit() 方法实现(源代码).
FYI, I've figured this out from the quit() method implementation (source code).