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位.
这似乎是selenium 3.0
版本中的一个错误
更新quit()
方法定义webdriver.py
的firefox
如下(相对路径:..\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_profile
和firefox_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
归档时间: |
|
查看次数: |
2357 次 |
最近记录: |