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 > aclass='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

相关推荐
在学了加油4 分钟前
Inception v1学习笔记
笔记·python·学习
Cthy_hy9 分钟前
Python算法竞赛:集合去重+字典映射 核心用法一站式整理
数据结构·python·算法
索西引擎10 分钟前
【langchain 1.0】ChromaDB 原生 API 实战:为 LangChain 向量库打造管理工具集
python·ai·langchain
Sirius.z11 分钟前
第J6周:Inception v1算法实战
python
山上三树11 分钟前
Python 高频报错速查表(开发通用版)
开发语言·python
Wonderful U13 分钟前
AI智能日志异常检测告警平台:告别人工排查,秒级定位线上故障
数据库·人工智能·python·django
MY_TEUCK16 分钟前
【MYTRUCK - AI 应用】MetaGPT 0.8.2 安装与排错完整实录(Python 3.10 + 虚拟环境)
开发语言·人工智能·python·ai
广_23 分钟前
用AI写一个Python实时硬件监控与日志可视化界面
开发语言·人工智能·python
weixin_4684668527 分钟前
机器学习与深度学习新手区分指南
人工智能·python·深度学习·机器学习·计算机视觉·ai·机器视觉
AI算法沐枫28 分钟前
基于YOLO26深度学习的【果园荔枝检测与计数】系统设计与实现【python源码+Pyqt5界面+数据集+训练代码】
开发语言·人工智能·python·深度学习·qt·学习·机器学习