繁体   English   中英

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

[英]How to avoid getting `'NoneType' object has no attribute 'path'` on selenium quit()?

运行Selenium Webdriver Python脚本时,执行self.driver.quit().将得到一个'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

$

有谁知道如何避免'NoneType' object has no attribute 'path'消息?

注意:
由于此问题已在11月初报告(请参见下面的URL),因此现在应该已经有补丁-但是将seleniumpip升级到最新版本并不能消除它。

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

selenium 3.0版本中似乎有一个错误

如下更新firefox webdriver.py中的quit()方法定义(相对路径: ..\\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)

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.

注意 :无论在何处使用self.profileself.profile这样做。 即,将代码移至上述的if条件。


Selenium 3.0profilebinary移至firefox_options而不是在Selenium 2.0分别作为firefox_profilefirefox_binary存在。

您可以在__init__方法的webdriver.pyfirefox )中对此进行验证。

__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.

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM