用selenium scrapy,webdriver无法实例化

pad*_*pad 6 python selenium scrapy phantomjs selenium-webdriver

我正在尝试使用selenium/phantomjs和scrapy,我充满了错误.例如,请使用以下代码段:

def parse(self, resposne):

    while True:
        try:
            driver = webdriver.PhantomJS()
            # do some stuff
            driver.quit()
            break
        except (WebDriverException, TimeoutException):
            try:
                driver.quit()
            except UnboundLocalError:
                print "Driver failed to instantiate"
            time.sleep(3)
            continue
Run Code Online (Sandbox Code Playgroud)

很多时候驱动程序似乎无法实例化(因此driver未绑定,因此异常),我得到了模糊(以及我输入的打印消息)

Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.phantomjs.service.Service object at 0x7fbb28dc17d0>> ignored
Run Code Online (Sandbox Code Playgroud)

谷歌搜索,似乎每个人都建议更新phantomjs,我有(1.9.8从源码建立).有谁知道还有什么可能导致这个问题和适当的诊断?

ale*_*cxe 6

这种行为的原因是如何实现PhantomJS驱动程序的Service.

有一个__del__定义了调用self.stop()方法的方法:

def __del__(self):
    # subprocess.Popen doesn't send signal on __del__;
    # we have to try to stop the launched process.
    self.stop()
Run Code Online (Sandbox Code Playgroud)

并且,self.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

    ...
Run Code Online (Sandbox Code Playgroud)

在这个帖子中完美地描述了同样的问题:


你应该做的是AttributeError在退出驱动程序实例时默默地忽略发生:

try:
    driver.quit()
except AttributeError:
    pass
Run Code Online (Sandbox Code Playgroud)

修订引入了这个问题.这意味着降级2.40.0也会有所帮助.