我正在尝试将 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
未绑定,因此是例外),并且我得到了简介(以及我输入的打印消息)
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 https://code.google.com/p/selenium/source/browse/py/selenium/webdriver/phantomjs/service.py已实施。
有一个__del__
定义的方法调用self.stop()
method:
def __del__(self):
# subprocess.Popen doesn't send signal on __del__;
# we have to try to stop the launched process.
self.stop()
And, 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
...
该线程完美地描述了同样的问题:
- __del__ 上的 Python 属性错误 https://stackoverflow.com/questions/18058730/python-attributeerror-on-del
你应该做的就是默默无视AttributeError
退出驱动程序实例时发生:
try:
driver.quit()
except AttributeError:
pass
问题是这样引入的revision https://code.google.com/p/selenium/issues/detail?id=7304。这意味着降级为2.40.0
也会有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)