‘NoneType‘ object has no attribute ‘...‘
程序员文章站
2022-07-13 10:51:42
...
‘NoneType’ object has no attribute ‘get’;
‘NoneType’ object has no attribute ‘switch_to’;
‘NoneType’ object has no attribute ‘find_element_by_name’;
等等这些错误,其实就是识别不出来
我报这个错的原因就是:没有声明driver是个全局变量【get,switch_to,find_element_by_name都是driver的方法】
更改之前的代码:
import unittest
from time import sleep
from selenium import webdriver
driver=None
class unitTestMock(unittest.TestCase):
@classmethod
def setUpClass(cls):
driver = webdriver.Chrome()
driver.maximize_window()
print("运行setUpClass...")
def setUp(self):
print("运行setUp...")
def test1_WebTours(self):
driver.get("http://localhost:1080/WebTours/")
sleep(3)
def test2_ClickSignOff(self):
driver.switch_to.default_content()
driver.switch_to.frame("body")
driver.switch_to.frame("navbar")
driver.find_element_by_xpath("//img[@alt=\"SignOff Button\"]")
def tearDown(self):
print("运行tearDown...")
@classmethod
def tearDownClass(cls):
driver.quit()
print("运行tearDownClass...")
if __name__=="__main__":
unittest.main(verbosity=2)
更改之后的代码:
import unittest
from time import sleep
from selenium import webdriver
driver=None
class unitTestMock(unittest.TestCase):
@classmethod
def setUpClass(cls):
global driver
driver = webdriver.Chrome()
driver.maximize_window()
print("运行setUpClass...")
def setUp(self):
print("运行setUp...")
def test1_WebTours(self):
driver.get("http://localhost:1080/WebTours/")
sleep(3)
def test2_ClickSignOff(self):
driver.switch_to.default_content()
driver.switch_to.frame("body")
driver.switch_to.frame("navbar")
driver.find_element_by_xpath("//img[@alt=\"SignOff Button\"]")
def tearDown(self):
print("运行tearDown...")
@classmethod
def tearDownClass(cls):
driver.quit()
print("运行tearDownClass...")
if __name__=="__main__":
unittest.main(verbosity=2)
差别就是在 setUpClass 方法中增加了一个 global driver。没有global driver,它就认为driver只是你定义的一个变量,自然也就没有webdriver的那些方法,即使你调用了也识别不出来。
上一篇: 记一次数据处理效率优化过程
推荐阅读
-
module ‘seaborn‘ has no attribute ‘scatterplot‘解决方案
-
AttributeError: module ‘community‘ has no attribute ‘best_partition‘ 问题解决方法
-
module ‘community‘ has no attribute ‘best_partition‘ [已解决]
-
【python】解决AttributeError: module ‘scipy.misc‘ has no attribute ‘toimage‘问题
-
【Tensorflow】Linux下Tensorflow报错:AttributeError: module ‘tensorflow‘ has no attribute ‘xxxx‘
-
AttributeError: 'module' object has no attribute 'main'
-
mongdb "errmsg" : "exception: 'out' has to be a string or an object"
-
TypeError: the JSON object must be str, bytes or bytearray, not NoneType
-
python3_opencv_直线检测_HoughLinesP_ 'NoneType' object is not subscriptable
-
‘NoneType‘ object is not subscriptable报错