如何隐藏Chromedriver控制台窗口? [英] How to hide Chromedriver console window?

查看:3992
本文介绍了如何隐藏Chromedriver控制台窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Python脚本,它使用selenium和webdriver在Chrome窗口中打开Facebook并自动登录。
当我运行它时,即使在整个程序执行完毕后,Chromedriver控制台窗口也会打开并保持打开状态,直到我自己关闭它为止。

有没有办法隐藏这个控制台窗口?我已经尝试为脚本保留一个.pyw扩展名,但这并没有帮助,因为它不是脚本的控制台窗口,而是我想隐藏的Chromedriver子进程的控制台窗口。



我找不到任何资源。我想我可能需要修改chrome webdriver源代码,但我不知道如何。这是我的代码:

 来自selenium import webdriver 
import sys

driver = webdriver .Chrome(C:\Python27\Scripts\chromedriver.exe)

driver.get(https://www.facebook.com)

电子邮件= 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() 结尾处脚本:


quit()



关闭浏览器并关闭在启动ChromeDriver时启动的ChromeDriver可执行文件


如果您只需关闭服务可执行文件并让浏览器保持打开状态,请致电:

  driver.service.stop()

仅供参考,我从> quit()方法实施(来源代码)。


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

解决方案

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

这篇关于如何隐藏Chromedriver控制台窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆