简体   繁体   English

如何使用 Python 退出通过 GeckoDriver 和 Selenium 启动的所有 Firefox 进程

[英]How to quit all the Firefox processes which gets initiated through GeckoDriver and Selenium using Python

I am using python 3.7 + selenium + geckodriver.exe + firefox 70.0.1x64,I know driver.quit() could close firefox window,but in some situation I can not quit firefox totally,I do not know why,this is my code: I am using python 3.7 + selenium + geckodriver.exe + firefox 70.0.1x64,I know driver.quit() could close firefox window,but in some situation I can not quit firefox totally,I do not know why,this is my code :

from selenium import webdriver
import time

def func1():
    driver = webdriver.Firefox()
    i = 0
    while True:
        try:
            if i > 10 and driver is not None:
                driver.quit()
                driver = None
                print('quit success')
            i += 1
            print(i)
            time.sleep(1)
        except KeyboardInterrupt:
            if driver is not None:
                driver.quit()
                driver = None
                print('keyboard quit success')

if __name__ == '__main__':
    func1()

With this code,there are tow ways to close firefox window:使用此代码,有两种方法可以关闭 firefox window:

1-wait 10 seconds. 1-等待 10 秒。

2-use Ctrl+C. 2-使用 Ctrl+C。

Then I test method 1然后我测试方法1

python test1.py

When firefox winodw showed,I notice there are 6 process in task manager,like this当 firefox winodw 显示时,我注意到任务管理器中有 6 个进程,像这样在此处输入图像描述

Then I wait 10 seconds,everything is fine,6 process is gone,like this:然后我等了 10 秒,一切都很好,6 个进程消失了,像这样:

在此处输入图像描述

Then I test method 2然后我测试方法2

I press Ctrl+C in 10 seconds,result like this:我在 10 秒内按 Ctrl+C,结果如下:

在此处输入图像描述

You can see that there are still 5 process alive,only 1 process is gone,I do not know why,Can anyone help me?Thanks a lot!你可以看到还有5个进程活着,只有1个进程没了,我不知道为什么,谁能帮助我吗?非常感谢!

When automated tests are getting executed through Mozilla Firefox as you have observed that there are potentially half a dozen of Mozilla Firefox processes are running which can be observed through Windows Task Manager's Processes tab.当通过Mozilla Firefox执行自动化测试时,正如您所观察到的,可能有六个Mozilla Firefox进程正在运行,这可以通过Processes任务管理器选项卡观察到。

多进程火狐


Multi-Process Firefox多进程 Firefox

As per the article Multi-Process Firefox: everything you need to know to improve the browser's stability, performance and security Firefox had increased the number of content processes to four for the stable population in Firefox and is now multi-process Firefox which is also known as Electrolysis or e10S . As per the article Multi-Process Firefox: everything you need to know to improve the browser's stability, performance and security Firefox had increased the number of content processes to four for the stable population in Firefox and is now multi-process Firefox which is also known作为电解e10S Multi-process architecture helps in separating tasks into processes as Electrolysis functionality hosts, renders, or executes web related content in background child processes which communicate with the "parent" Firefox browser via various ipdl protocols.多进程架构有助于将任务分离为进程,因为电解功能在后台子进程中托管、呈现或执行 web 相关内容,这些子进程通过各种 ipdl 协议与“父”Firefox 浏览器通信。 Additionally, multi-process Firefox moves NPAPI plugins, media playback and web content to child processes thus separating them from the browser's core.此外,多进程 Firefox 将 NPAPI 插件、媒体播放和 web 内容移动到子进程,从而将它们与浏览器的核心分离。


Details细节

You can observe that several firefox.exe processes when you run a process manager, ie the Windows Task Manager which essentially implies that Electrolysis is enabled.当您运行进程管理器时,您可以观察到几个firefox.exe进程,即 Windows 任务管理器,这实际上意味着启用了电解 Apparently Firefox should run just like before but ideally enabling multi-process functionality should improve the browser's performance and stability right away.显然 Firefox 应该像以前一样运行,但理想情况下启用多进程功能应该会立即提高浏览器的性能和稳定性。 However, you may however notice a higher than usual RAM usage.但是,您可能会注意到比平常更高的 RAM 使用率。 Mozilla confirmed that Firefox with Electrolysis will use about 20% more RAM . Mozilla 证实,带有电解功能的 Firefox 将使用大约20%RAM You may change how many processes Firefox uses for its multi-process functionality.您可以更改 Firefox 用于其多进程功能的进程数。


This usecase这个用例

In your first usecase when you invoke driver.quit() the parent and all the child Firefox processes and the parent Firefox process gets destroyed programatically.在您调用driver.quit()的第一个用例中,父进程和所有子 Firefox 进程和父 Firefox 进程以编程方式被破坏。

You can find a detailed discussion in Selenium: How to stop geckodriver process impacting PC memory, without calling driver.quit()?您可以在Selenium 中找到详细讨论:如何停止 geckodriver 进程影响 PC memory,无需调用 driver.quit()?

In your second usecase, you have pressed Ctrl and c after 4 was printed, when KeyboardInterrupt occurs and your program is aborted and presumably GeckoDriver looses the control of the Mozilla Firefox browser process.在您的第二个用例中,您在打印4后按下了Ctrlc ,此时KeyboardInterrupt发生并且您的程序被中止并且可能GeckoDriver失去了对Mozilla Firefox浏览器进程的控制。 However, still 1 process gets terminated through driver.quit() but the rest processes remains dangling.但是,仍然有1 个进程通过driver.quit()终止,但 rest 进程仍然悬空。


Outro奥特罗

You can find a relevant discussion in Many process of Google Chrome (32 bit)您可以在Google Chrome 的许多进程(32 位)中找到相关讨论

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 GeckoDriver 和 Selenium 到 ZA7F5F35426B927411FC9231B56382173 启动使用默认 Firefox 到 68.9.0esr 的 Tor 浏览器 9.5 - How to initiate a Tor Browser 9.5 which uses the default Firefox to 68.9.0esr using GeckoDriver and Selenium through Python 如何使用 GeckoDriver 和 Firefox 到 Python 检测不到 Selenium 脚本? - How can I make a Selenium script undetectable using GeckoDriver and Firefox through Python? 'geckodriver' 可执行文件需要在 PATH 中使用 GeckoDriver 和 Firefox 到 Selenium - 'geckodriver' executable needs to be in PATH using GeckoDriver and Firefox through Selenium Python Selenium 和 FireFox \\ geckodriver - Python Selenium and FireFox \ geckodriver selenium.common.exceptions.InvalidSessionIdException 使用 GeckoDriver Selenium Firefox 在无头模式下通过 Python - selenium.common.exceptions.InvalidSessionIdException using GeckoDriver Selenium Firefox in headless mode through Python 类型错误:get() 缺少 1 个必需的位置参数:使用 GeckoDriver 和 Firefox 到 Selenium 和 Python 时出现“url”错误 - TypeError: get() missing 1 required positional argument: 'url' error using GeckoDriver and Firefox through Selenium and Python OSError: [WinError 193] %1 is not a valid Win32 application error using GeckoDriver and Firefox through Selenium and Python on Windows - OSError: [WinError 193] %1 is not a valid Win32 application error using GeckoDriver and Firefox through Selenium and Python on Windows 使用 GeckoDriver Firefox 到 Selenium 已丢弃浏览上下文 - Browsing context has been discarded using GeckoDriver Firefox through Selenium 使用GeckoDriver和Python的Selenium firefox webdriver未知错误 - Selenium firefox webdriver unknown error using GeckoDriver and Python 无法使用Selenium-python-geckodriver打开Firefox - Unable to open firefox using selenium-python-geckodriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM