Selepy with Selenium,Webdriver无法实例化 [英] Scrapy with selenium, webdriver failing to instantiate
问题描述
我正尝试使用selenium/phantomjs,而且出现了很多错误.例如,使用以下代码段:
I am trying to use selenium/phantomjs with scrapy and I'm riddled with errors. For example, take the following code snippet:
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
是未绑定的,因此是例外),我得到了blurb(以及我输入的打印消息)
A lot of the times the driver it seems it has failed to instantiate (so the driver
is unbound, hence the exception), and I get the blurb (along with the print message I put in)
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
).有人知道还会导致此问题和适当的诊断吗?
Googling around, it seems everyone suggests updating phantomjs, which I have (1.9.8
built from source). Would anyone know what else could be causing this problem and a suitable diagnosis?
推荐答案
The reason for this behavior is how the PhantomJS driver's Service
class is implemented.
定义了一个__del__
方法,该方法调用self.stop()
方法:
There is a __del__
method defined that calls 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()
并且,self.stop()
假定服务实例仍在尝试访问其属性时仍然存在:
And, self.stop()
is assuming the service instance is still alive trying to access it's attributes:
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
...
此线程完全描述了相同的确切问题:
The same exact problem is perfectly described in this thread:
您应该做的是静默忽略退出驱动程序实例时出现的AttributeError
:
What you should do is to silently ignore AttributeError
occurring while quitting the driver instance:
try:
driver.quit()
except AttributeError:
pass
此问题是由修订版引入的.这意味着降级为2.40.0
也会有所帮助.
The problem was introduced by this revision. Which means that downgrading to 2.40.0
would also help.
这篇关于Selepy with Selenium,Webdriver无法实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!