selenium获取元素 出现的错误AttributeError: ‘TestPage‘ object has no attribute ‘driver‘

目录

错误收集

错误分析

解决办法

运行结果


错误收集

D:\Program\Util\python.exe "D:/Program/myUtil/PyCharm 2024.3.5/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target testfeishu.py::TestPage

Testing started at 8:04 ...

Launching pytest with arguments testfeishu.py::TestPage --no-header --no-summary -q in C:\Users\Administrator\PycharmProjects\PoPage

============================= test session starts =============================

collecting ... collected 1 item

testfeishu.py::TestPage::test_login_001

============================== 1 failed in 0.37s ==============================

FAILED [100%]你好啊

testfeishu.py:23 (TestPage.test_login_001)

self = <testfeishu.TestPage object at 0x000001D93AB3E710>

def test_login_001(self):

print("你好啊")

获取元素 ==》通过id定位

> login_button = self.driver.find_element(by=By.CSS_SELECTOR, value="div > a[class='button-login']")

^^^^^^^^^^^

E AttributeError: 'TestPage' object has no attribute 'driver'

testfeishu.py:27: AttributeError

Process finished with exit code 1

错误分析

查看代码,发现我们写的前置处理函数是setup , 但是这个是当前最新版本的selenium 这个方法已经被修改为setup_method,teardown也已经被修改为teardown_method

复制代码
def setup(self):
    self.driver = webdriver.Chrome()

解决办法

修改前置处理器的方法

def setup_class(self)

复制代码
class TestPage:

    def setup_method(self):
        self.driver = webdriver.Chrome()

运行结果

可以看到报错已经解决了

运行结果收集

D:\Program\Util\python.exe "D:/Program/myUtil/PyCharm 2024.3.5/plugins/python-ce/helpers/pycharm/_jb_pytest_runner.py" --target testfeishu.py::TestPage.test_login_001

Testing started at 8:15 ...

Launching pytest with arguments testfeishu.py::TestPage::test_login_001 --no-header --no-summary -q in C:\Users\Administrator\PycharmProjects\PoPage

============================= test session starts =============================

collecting ... collected 1 item

testfeishu.py::TestPage::test_login_001

============================== 1 passed in 6.85s ==============================

PASSED [100%]你好啊

已经获取了驱动

Process finished with exit code 0

相关推荐
hui函数2 小时前
Flask电影投票系统全解析
后端·python·flask
闲人编程3 小时前
Python第三方库IPFS-API使用详解:构建去中心化应用的完整指南
开发语言·python·去中心化·内存·寻址·存储·ipfs
计算机编程小咖4 小时前
《基于大数据的农产品交易数据分析与可视化系统》选题不当,毕业答辩可能直接挂科
java·大数据·hadoop·python·数据挖掘·数据分析·spark
zhangfeng11335 小时前
以下是基于图论的归一化切割(Normalized Cut)图像分割工具的完整实现,结合Tkinter界面设计及Python代码示
开发语言·python·图论
flashlight_hi6 小时前
LeetCode 分类刷题:2529. 正整数和负整数的最大计数
python·算法·leetcode
Ashlee_code6 小时前
香港券商櫃台系統跨境金融研究
java·python·科技·金融·架构·系统架构·区块链
Jia-Hui Su7 小时前
GDSFactory环境配置(PyCharm+Git+KLayout)
git·python·pycharm
学习3人组8 小时前
手写数字识别代码
人工智能·python
古译汉书8 小时前
蓝桥杯算法之基础知识(2)——Python赛道
数据结构·python·算法·蓝桥杯
少陵野小Tommy8 小时前
Python能用古诗词数据库做什么7:根据标题、诗句查找诗歌
开发语言·数据库·python