‘NoneType‘ object has no attribute ‘...‘
程序员文章站
2022-07-02 22:30:25
‘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...
‘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的那些方法,即使你调用了也识别不出来。
本文地址:https://blog.csdn.net/qq_43096786/article/details/110878233
上一篇: 手写算法-python代码实现逻辑回归(带L1、L2正则项)
下一篇: 《Python核心编程》练习题之2-8:更新上一个练习的解决方案,修改它以使你的聊天服务现在成为全双工模式,意味着通信两端都可以发送并接收消息,并且二者相互独立
推荐阅读
-
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报错