如何避免获得''NoneType'对象在selenium quit()上没有属性'path'`?

boa*_*der 5 python selenium exception webdriver selenium-webdriver

当运行硒webdriver的Python脚本,一个得到了'NoneType' object has no attribute 'path'执行后self.driver.quit().
self.driver.quit()try/except没有帮助,即:

$ cat demo_NoneType_attribute_error.py
# -*- coding: utf-8 -*-
from selenium import webdriver
import unittest

class TestPass(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_pass(self):
        pass

    def tearDown(self):
        print("doing: self.driver.quit()")
        try:
            self.driver.quit()
        except AttributeError:
            pass

if __name__ == "__main__":
    unittest.main()

$ python demo_NoneType_attribute_error.py
doing: self.driver.quit()
'NoneType' object has no attribute 'path'
.
----------------------------------------------------------------------
Ran 1 test in 19.807s

OK

$
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何避免这个'NoneType' object has no attribute 'path'消息?

注意:
由于此问题已在11月初报告(请参阅下面的URL),它现在应该有一个补丁 - 但升级selenium到最新版本pip并没有消除它.

环境:硒3.0.2; Python 2.7; Windows 7上的Cygwin 32位.

Nav*_*R B 6

这似乎是selenium 3.0版本中的一个错误

更新quit()方法定义webdriver.pyfirefox 如下(相对路径:..\Python27\Lib\site-packages\selenium\webdriver\firefox\webdriver.py):

更改quit()方法中的以下行:

shutil.rmtree(self.profile.path) #which gives Nonetype has no attribute path
if self.profile.tempfolder is not None:
    shutil.rmtree(self.profile.tempfolder)
Run Code Online (Sandbox Code Playgroud)

if self.profile is not None:
    shutil.rmtree(self.profile.path) # if self.profile is not None, then only rmtree method is called for path.
    if self.profile.tempfolder is not None:
        shutil.rmtree(self.profile.tempfolder) # if tempfolder is not None, then only rmtree is called for tempfolder.
Run Code Online (Sandbox Code Playgroud)

注意:无论在哪里self.profile使用,都要这样做.即,将代码移动到if条件,如上所述.


Selenium 3.0,profile并且binary转移到firefox_options而不是分别存在firefox_profilefirefox_binary分别存在Selenium 2.0.

您可以验证这一点webdriver.py(的firefox)的__init__方法.

__init__方法中的相关代码:

if firefox_options is None:
    firefox_options = Options()
    print dir(firefox_options) # you can refer binary and profile as part of firefox_options object.
Run Code Online (Sandbox Code Playgroud)

注意:观察到firefox_options.profile仍然给出None,这可能是一个需要解决的问题selenium 3.0