Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly stop phantomjs execution

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?

like image 422
CptNemo Avatar asked Aug 04 '14 01:08

CptNemo


People also ask

How do I stop PhantomJS EXE?

You call quit() in parent(selenium), it send SIGTERM to child(phantomjs). and a child(phantomjs) send SIGTERM to grandchild(lib/phantomjs. js) in the child's SIGTERM handler function. A grandchild will be zombie when parent send SIGKILL to child before the child send SIGTERM to grandchild.

Does Selenium support PhantomJS?

Selenium considers PhantomJS as deprecated, so you need to us either Chrome or Firefox in headless mode.

What is PhantomJS in Selenium?

PhantomJS is a headless Webkit, which has a number of uses. In this example, we'll be using it, in conjunction with Selenium WebDriver, for conducting basic system tests directly from the command line. Since PhantomJS eliminates the need for a graphical browser, tests run much faster.

What is PhantomJS driver?

PhantomJS is a webkit which runs headless with an inbuilt JavaScript API. It has fast and native support for various web standards such as DOM handling, CSS selector, and JSON. It is quite fast compared to running tests using the Selenium web driver.


2 Answers

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 
like image 108
leekaiinthesky Avatar answered Nov 04 '22 07:11

leekaiinthesky


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 
like image 37
whirlwin Avatar answered Nov 04 '22 07:11

whirlwin