[英]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
任务管理器选项卡观察到。
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 内容移动到子进程,从而将它们与浏览器的核心分离。
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 用于其多进程功能的进程数。
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后按下了Ctrl和c ,此时
KeyboardInterrupt
发生并且您的程序被中止并且可能GeckoDriver失去了对Mozilla Firefox浏览器进程的控制。 However, still 1 process gets terminated through driver.quit()
but the rest processes remains dangling.但是,仍然有1 个进程通过
driver.quit()
终止,但 rest 进程仍然悬空。
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.