Python AttributeError:“非类型”对象没有属性“关闭”

Python AttributeError:“非类型”对象没有属性“关闭”,python,selenium-webdriver,phantomjs,Python,Selenium Webdriver,Phantomjs,我是PythonJS新手,我正在尝试运行一个在上可用的项目 但当我试图运行它时,会出现以下错误 Traceback (most recent call last): File "main.py", line 81, in <module> crawler_machine() File "main.py", line 76, in crawler_machine driver.close() AttributeError: 'NoneType' obje

我是PythonJS新手,我正在尝试运行一个在上可用的项目

但当我试图运行它时,会出现以下错误

Traceback (most recent call last):   File "main.py", line 81, in
 <module>
     crawler_machine()   File "main.py", line 76, in crawler_machine
     driver.close()  AttributeError: 'NoneType' object has no attribute 'close' 
Exception AttributeError: "'Service' object has no attribute
 'process'" in <bound method Service.__del__ of
 <selenium.webdriver.phantomjs.service.Service object at 0x107d05790>>
 ignored
如果PhantomJS和Firefox的创建都失败,则驱动程序不会初始化

关闭前添加支票:

if driver:
    driver.close()

我的猜测是,创建驱动程序的两次尝试——PhantomJS和FireFox——都失败了,这意味着当您到达finally块时,驱动程序仍然没有。您可以通过添加显式检查来确认这一点:

    finally:
        if driver:
            driver.close()
        else:
            print "Could not create driver"
至于为什么驱动程序创建会失败,最有可能的解释是安装问题。您可以通过在Python环境中运行一个简单的示例来测试这一点:

>>> from selenium.webdriver import Firefox
>>> d = Firefox()
>>> d.get('http://stackoverflow.com')

如果这没有打开Firefox并导航到Stack Overflow的首页,请查看操作系统的Selenium文档。

如果在crawler\u机器中try…except子句都失败,则驱动程序的全局值将为None。

@user3946530是否已修复?
>>> from selenium.webdriver import Firefox
>>> d = Firefox()
>>> d.get('http://stackoverflow.com')