[英]Python3, Selenium, Chromedriver console window
I've a made a selenium test using python3 and selenium library. 我已经使用python3和selenium库进行了硒测试。
I've also used Tkinter to make a GUI to put some input on (account, password..). 我还使用Tkinter制作了一个GUI,以在(帐户,密码..)上输入一些内容。
I've managed to hide the console window for python by saving to the .pyw extension; 通过保存到.pyw扩展名,我设法隐藏了python的控制台窗口。 and when I make an executable with my code, the console doesn't show up even if it's saved with .py extension.
当我用代码制作可执行文件时,即使以.py扩展名保存,控制台也不会显示。
However, everytime the chromedriver starts, it also starts a console window, and when the driver exists, this window does not. 但是,每次chromedriver启动时,它也会启动一个控制台窗口,而当驱动程序存在时,该窗口不存在。
so in a loop, i'm left with many webdriver consoles. 因此,在一个循环中,我剩下许多WebDriver控制台。
Is there a work around this to prevent the driver from launching a console everytime it runs ? 是否有解决方法来防止驱动程序每次运行时启动控制台?
I hated dealing with this in selenium until I remembered that this was an obvious use case for context managers just like the usage of open
. 我讨厌用硒处理这个问题,直到我想起这就像上下文对
open
的用法一样,对于上下文管理器来说是一个显而易见的用例。
I did find out that selenium is about to add this officially to their package in this pull request 我确实发现硒将在此请求中正式将其添加到其包装中
Until this is officially added, this snippet should give you the functionality you need to get things going :) 在正式添加此代码之前,此代码段应为您提供所需的功能:)
import contextlib
@contextlib.contextmanager
def Chrome(*args, **kwargs):
webdriver = webdriver.Chrome(*args, **kwargs)
try:
yield webdriver
finally:
webdriver.quit()
with Chrome() as driver:
# whatever you're planning on doing goes here
driver.close()
and driver.quit()
are two different methods for closing the browser session in Selenium WebDriver. driver.close()
和driver.quit()
是在Selenium WebDriver中关闭浏览器会话的两种不同方法。
driver.close()
- It closes the the browser window on which the focus is set. driver.close()
-关闭设置焦点的浏览器窗口。
driver.quit()
– It basically calls driver.dispose method which in turn closes all the browser windows and ends the WebDriver session gracefully. driver.quit()
–它基本上调用了driver.dispose方法,该方法又关闭了所有浏览器窗口并优雅地结束了WebDriver会话。
You should use driver.quit whenever you want to end the program. 每当您要结束程序时,都应使用driver.quit。 It will close all opened browser window and terminates the WebDriver session.
它将关闭所有打开的浏览器窗口,并终止WebDriver会话。 If you do not use driver.quit at the end of program, WebDriver session will not close properly and files would not be cleared off memory.
如果在程序末尾不使用driver.quit,WebDriver会话将无法正确关闭,并且文件也不会从内存中清除。 This may result in memory leak errors.
这可能会导致内存泄漏错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.