[英]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),因此现在应该已经有补丁-但是将selenium
从pip
升级到最新版本并不能消除它。
环境:硒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.profile
, self.profile
这样做。 即,将代码移至上述的if条件。
在Selenium 3.0
, profile
和binary
移至firefox_options
而不是在Selenium 2.0
分别作为firefox_profile
和firefox_binary
存在。
您可以在__init__
方法的webdriver.py
( firefox
)中对此进行验证。
__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.