简体   繁体   中英

How to close Headless Firefox after test execution using Selenium and Python

I've been following this tutorial to learn how to use Selenium and I just successfully ran the following code:

from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options

opts = Options()opts.headless=True
assert opts.headless # Operating in headless mode
browser = Firefox(options=opts)browser.get('https://bandcamp.com')
browser.find_element_by_class('playbutton').click()

How do I ensure the headless Firefox is no longer running? I ran this code twice and now two songs are playing over each other. Any help is appreciated, I just want to make sure nothing is running in the background!

From the tutorial you shared:

Everything seems to be working. In order to prevent invisible headless browser instances from piling up on your machine, you close the browser object before exiting your Python session:

Try to do the following to see if your issues are fixed:

browser.close()
quit()

EDIT:

Let's straight this out. So based on your request, we currently have:

from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium import webdriver # you need this to terminate program 

options = Options() 
options.headless = True

driver = webdriver.Firefox(options=options)

driver.get('https://bandcamp.com')

driver.find_element_by_class('playbutton').click()

ch = input('Do you want to quit the program y/n?')
if ch == 'y':
    driver.quit()
    print('The program is terminated')

Let me know if the solution doesn't work

Just add

browser.close()

Also you can check the process list (eg lsof in Ubuntu)

You can add add browser.close() to close the active tab and browser.quit() to closes all browser windows and ends driver's session/process.

Irespective of GUI based browser or browser, at the end of your test execution you should always invoke browser.quit() which calls the /shutdown endpoint and subsequently the WebDriver instance and the Browsing Context both are destroyed completely closing all the pages/tabs/windows.

So your effective code block will be:

# previous lines of code
browser.find_element_by_class('playbutton').click()
browser.quit()

You can find a detailed discussion in PhantomJS web driver stays in memory


However, in the rarest of the rare cases there may be residue instances of WebDriver eg ChromeDriver occupying the memory, in those cases you need to kill them with brute force before triggering the next test execution as follows:

  • Python Solution (Windows):

     import os os.system("taskkill /f /im geckodriver.exe /T") os.system("taskkill /f /im chromedriver.exe /T") os.system("taskkill /f /im IEDriverServer.exe /T")
  • Python Solution (Cross Platform):

     import os import psutil PROCNAME = "geckodriver" # or chromedriver or IEDriverServer for proc in psutil.process_iter(): # check whether the process name matches if proc.name() == PROCNAME: proc.kill()

You can find a detailed discussion in Selenium: How to stop geckodriver process impacting PC memory, without calling driver.quit()?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM