简体   繁体   English

Python3,Selenium,Chromedriver控制台窗口

[英]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.

相关问题 带有 python3、chromedriver、chrome 和 selenium 的 Docker 镜像 - Docker image with python3, chromedriver, chrome & selenium Selenium 和 Python3 ChromeDriver 引发消息:无法连接到服务 chromedriver - Selenium and Python3 ChromeDriver raises Message: Can not connect to the Service chromedriver Python Selenium如何使用现有的chromedriver窗口? - Python Selenium how to use an existing chromedriver window? 如何使用python selenium chromedriver隐藏cmd窗口 - How to hide cmd window with python selenium chromedriver 为什么 selenium/python/chromedriver 无法识别 Chrome 开发人员控制台中的有效 xpath? - Why is valid xpath in chrome developer console not recognized by selenium / python / chromedriver? 如何使用 Python、Selenium Webdriver 和 Chromedriver 在 Jenkins 中最大化窗口? - How to maximize window in Jenkins using Python, Selenium Webdriver and Chromedriver? 如何设置硒窗口的大小? (在python中,在Mac上,使用chromedriver) - How to set the size of the selenium window? (in python, on Mac, using chromedriver) 使用未检测到的 chromedriver 打开第二个 window + selenium、python - opening the second window using undetected chromedriver + selenium, python python-selenium-chromedriver不启动任何可见窗口 - python-selenium-chromedriver not launching any visible window 在硒中打开之前设置chromedriver窗口大小(Python) - Set chromedriver window size before opening in selenium (python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM