python - Scrapy with selenium, webdriver 无法实例化

标签 python selenium selenium-webdriver scrapy phantomjs

我正在尝试将 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

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

Exception AttributeError: "'Service' object has no attribute 'process'" in <bound method Service.__del__ of <selenium.webdriver.phantomjs.service.Service object at 0x7fbb28dc17d0>> ignored

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

最佳答案

此行为的原因是 PhantomJS 驱动程序的 Service class已实现。

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

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

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

    ...

这个线程中完美地描述了同样的问题:


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

try:
    driver.quit()
except AttributeError:
    pass

问题是由这个 revision 引入的.这意味着降级到 2.40.0 也会有所帮助。

关于python - Scrapy with selenium, webdriver 无法实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27674088/

相关文章:

java - 使用 selenium 单击菜单选项

python-3.x - 如何在 Docker 容器内并行运行 Pytest 中的测试?

python - 使用 .htaccess 通过 Tornado 保护 Web 请求

python - Pandas 计算机每小时平均值并设置在间隔中间

javascript - 使用 Protractor 检查浏览器控制台中没有错误

javascript - Selenium webdriver - 页面末尾(使用垂直滚动)

C# Selenium Webdriver 异常 |此版本的 ChromeDriver 仅支持 Chrome 85 版

python - get_text() 或文本属性不适用于标签

python - PIL 和 import 声明是怎么回事

python - 使用 Python 解析多行数据文件