Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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:

1-wait 10 seconds.

2-use Ctrl+C.

Then I test method 1

python test1.py

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

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

enter image description here

Then I test method 2

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

enter image description here

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!

like image 292
LadyKiller2018 Avatar asked Nov 13 '19 05:11

LadyKiller2018


1 Answers

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.

Multi-Process Firefox


Multi-Process 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. 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.


Details

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.


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.

You can find a detailed discussion in Selenium : How to stop geckodriver process impacting PC memory, without calling 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.


Outro

You can find a relevant discussion in Many process of Google Chrome (32 bit)

like image 189
undetected Selenium Avatar answered Jan 04 '23 18:01

undetected Selenium