如何使用Python退出通过GeckoDriver和Selenium启动的所有Firefox进程 [英] How to quit all the Firefox processes which gets initiated through GeckoDriver and Selenium using Python

查看:262
本文介绍了如何使用Python退出通过GeckoDriver和Selenium启动的所有Firefox进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python 3.7 + selenium + geckodriver.exe + firefox 70.0.1x64,我知道driver.quit()可以关闭firefox窗口,但是在某些情况下我无法完全退出firefox,不知道为什么,这是我的代码:

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

使用此代码,有两种方法可以关闭Firefox窗口:

With this code,there are tow ways to close firefox window:

1次等待10秒.

2次使用Ctrl + C.

2-use Ctrl+C.

然后我测试方法1

python test1.py

当firefox winodw显示时,我注意到任务管理器中有6个过程,就像这样

When firefox winodw showed,I notice there are 6 process in task manager,like this

然后我等待10秒钟,一切都很好,6进程消失了,像这样:

Then I wait 10 seconds,everything is fine,6 process is gone,like this:

然后我测试方法2

我在10秒钟内按Ctrl + C,结果如下:

I press Ctrl+C in 10 seconds,result like this:

您可以看到仍有5个进程在运行,只有1个进程消失了,我不知道为什么,有人可以帮助我吗?

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!

推荐答案

通过 Mozilla Firefox 执行自动测试时,您可能已经发现许多 Mozilla Firefox 进程正在运行,可以通过Windows任务管理器的Processes选项卡进行观察.

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.

根据文章多进程Firefox:您需要的一切要知道,以提高浏览器的稳定性,性能和安全性. Firefox 已将稳定的Firefox中的内容进程数增加到了四个,现在是多进程Firefox,也称为多进程Firefox. 电解 e10S .多进程体系结构可帮助将任务分解为进程,因为 Electrolysis 功能可在后台子进程中托管,渲染或执行与Web相关的内容,这些子进程可通过各种ipdl协议与父" Firefox浏览器进行通信.此外,多进程Firefox将NPAPI插件,媒体播放和Web内容移至子进程,从而将其与浏览器的核心分开.

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. 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. Additionally, multi-process Firefox moves NPAPI plugins, media playback and web content to child processes thus separating them from the browser's core.

运行进程管理器(即Windows任务管理器)时,您会观察到多个firefox.exe进程,这实际上意味着已启用电解.显然,Firefox应该像以前一样运行,但是理想情况下启用多进程功能应该可以立即改善浏览器的性能和稳定性.但是,您可能会发现RAM的使用率比平常高. Mozilla确认,带电解的Firefox将使用约 20% RAM .您可以更改Firefox用于其多进程功能的进程数.

You can observe that several firefox.exe processes when you run a process manager, i.e. the Windows Task Manager which essentially implies that Electrolysis is enabled. Apparently Firefox should run just like before but ideally enabling multi-process functionality should improve the browser's performance and stability right away. However, you may however notice a higher than usual RAM usage. Mozilla confirmed that Firefox with Electrolysis will use about 20% more RAM. You may change how many processes Firefox uses for its multi-process functionality.

在第一个用例中,当您调用driver.quit()时,父级和所有子级Firefox进程以及父级Firefox进程都会以编程方式被销毁.

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.

您可以在在第二个用例中,当 KeyboardInterrupt 被打印后,在打印 4 后按下 Ctrl c >发生,并且您的程序被终止,并且可能是 GeckoDriver 失去了对 Mozilla Firefox 浏览器进程的控制.但是, 1 进程仍通过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. However, still 1 process gets terminated through driver.quit() but the rest processes remains dangling.

您可以在 Google的许多流程中找到相关的讨论Chrome(32位)

这篇关于如何使用Python退出通过GeckoDriver和Selenium启动的所有Firefox进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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