How to properly stop phantomjs execution

27,310

Solution 1

The .close() method is not guaranteed to release all resources associated with a driver instance. Note that these resources include, but may not be limited to, the driver executable (PhantomJS, in this case). The .quit() method is designed to free all resources of a driver, including exiting the executable process.

Solution 2

As of July 2016, driver.close() and driver.quit() weren't sufficient for me. That killed the node process but not the phantomjs child process it spawned.

Following the discussion on this GitHub issue, the single solution that worked for me was to run:

import signal

driver.service.process.send_signal(signal.SIGTERM) # kill the specific phantomjs child proc
driver.quit()                                      # quit the node proc

Solution 3

Please note that this will obviously cause trouble if you have several threads/processes starting PhantomJS on your machine.

I've seen several people struggle with the same issue, but for me, the simplest workaround/hack was to execute the following from the command line through Python AFTER you have invoked driver.close() or driver.quit():

pgrep phantomjs | xargs kill

Solution 4

I was having a similar issue on Windows machine. I had no luck with either

driver.close()

or

driver.quit()

actually closing out of the PhantomJS window, but when I used both, the PhantomJS window finally closed and exited properly.

driver.close()
driver.quit()

Solution 5

driver.quit() did not work for me on Windows 10, so I ended up adding the following line right after calling driver.close():

os.system('taskkill /f /im phantomjs.exe')

where

/f = force
/im = by image name

And since this is a Windows only solution, it may be wise to only execute if os.name == 'nt'

Share:
27,310
CptNemo
Author by

CptNemo

Updated on July 21, 2022

Comments

  • CptNemo
    CptNemo almost 2 years

    I initiated and close phantomjs in Python with the following

    from selenium import webdriver    
    driver = webdriver.PhantomJS()
    driver.get(url)
    html_doc = driver.page_source
    driver.close()
    

    yet after the script ends execution I still find an instance of phantomjs in my Mac Activity Monitor. And actually every time I run the script a new process phantomjs is created.

    How should I close the driver?

  • whirlwin
    whirlwin almost 9 years
    @Arya It will indeed cause problems with any other process/thread using PhantomJS, so you should not use this approach unless you have absolute control over what is running on your machine.
  • C.Buhl
    C.Buhl almost 8 years
    This also worked for me, and I think it should be marked as the correct solution. It's not elegant, but it is much better than the other brute force methods mentioned. Nice find.
  • rox
    rox over 7 years
    I tred both .close() and .quit(), didn't work for me
  • Geoff
    Geoff over 7 years
    This method works the best for me (as of this comment date). I also found the Github issue but wanted to second it here!
  • ntk4
    ntk4 over 7 years
    +1 this works for me too. A little frustrating when the GitHub unresolved issue with what seems like PhantomJS builders seem to have no interest in resolving this rather substantial bug with their script. Perhaps this solution could be hard coded into the PhantomJS script itself?
  • ntk4
    ntk4 over 7 years
    +1 this worked for me too. I like this solution a little bit more then @leekaiinthesky 's solution by kiling the service using signal because I don't have to import another library import signal
  • leekaiinthesky
    leekaiinthesky over 7 years
    See stackoverflow.com/questions/25110624/… for an answer that kills only the specific single phantomjs process involved rather than all of them.
  • Andrew Scott Evans
    Andrew Scott Evans over 7 years
    @rox Did you use both close and quit? I am still seeing this error but it seems to be non-fatal now.
  • likewhoa
    likewhoa about 7 years
    if you're going to cut corners then you might as well use killall phantomjs
  • Panagiotis Simakis
    Panagiotis Simakis almost 7 years
    until now, i had a fork bomb in my machine!
  • Ali Hesari
    Ali Hesari almost 7 years
    @leekaiinthesky But after ‍‍driver.quit() and driver.close() I use it, I receive the SessionManagerReqHand - _cleanupWindowlessSessions - Asynchronous Sessions clean-up phase starting NO messages. What is the reason How can I fix the problem ?!
  • leekaiinthesky
    leekaiinthesky almost 7 years
    @AliHesari Your best shot at getting that answered here would be to post your question with the details as a new question on the site. Good luck!
  • Tom
    Tom over 6 years
    @whirlwin Sorry, but I have to down-vote this given it'll be mostly new developers that follow this advice, which in turn will cause huge problems on shared machines with other developer's processes and unfair aggro in return. I'll up-vote though if the end-warning is moved to the start.
  • whirlwin
    whirlwin over 6 years
    @Tom Thanks for the feedback!