Selenium webdriver + PhantomJS进程未关闭[英] Selenium webdriver + PhantomJS processes not closing

本文是小编为大家收集整理的关于Selenium webdriver + PhantomJS进程未关闭的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这几乎是您可以使用WebDriver和Phantom进行的最简单的开放和关闭: from selenium import webdriver crawler = webdriver.PhantomJS() crawler.set_window_size(1024,768) crawler.get('https://www.google.com/') crawler.quit()

在Windows(7)上,每次我运行代码测试某些内容时,conhost.exe和phantomjs.s.exe过程的新实例开始并且永不退出.我在这里做一些愚蠢的事情吗?我认为当crawler.quit()做...

时,过程将退出

推荐答案

去数字.重新启动解决问题.

其他推荐答案

重新启动不是解决此问题的解决方案.我已经在Linux系统中实验了这个黑客.尝试修改service.py

中定义的stop()函数
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

添加了行send_signal明确表示信号退出phantomjs过程.不要忘记在此文件开始时添加import signal语句.

本文地址:https://www.itbaoku.cn/post/1740126.html

问题描述

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

推荐答案

Go figure. Problem resolved with a reboot.

其他推荐答案

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.