问题
Here's just about the simplest open and close you can do with webdriver and phantom:
from selenium import webdriver
crawler = webdriver.PhantomJS()
crawler.set_window_size(1024,768)
crawler.get('https://www.google.com/')
crawler.quit()
On windows (7), every time I run my code to test something out, new instances of the conhost.exe and phantomjs.exe processes begin and never quit. Am I doing something stupid here? I figured the processes would quit when the crawler.quit()
did...
回答1:
Go figure. Problem resolved with a reboot.
回答2:
Rebooting is not a solution for this problem. I have experimented this hack in LINUX system. Try modifying the stop()
function defined in service.py
def stop(self):
"""
Cleans up the process
"""
if self._log:
self._log.close()
self._log = None
#If its dead dont worry
if self.process is None:
return
#Tell the Server to properly die in case
try:
if self.process:
self.process.stdin.close()
#self.process.kill()
self.process.send_signal(signal.SIGTERM)
self.process.wait()
self.process = None
except OSError:
# kill may not be available under windows environment
pass
Added line send_signal
explicitly to give the signal to quit phantomjs process. Don't forget to add import signal
statement at start of this file.
来源:https://stackoverflow.com/questions/26868492/selenium-webdriver-phantomjs-processes-not-closing