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

相关推荐
软件测试-阿涛17 分钟前
软件测试开发转型经验分享与职业发展指南
经验分享·python·功能测试·ci/cd·自动化·jenkins·持续集成
whabc10021 分钟前
ubuntu24.04安装selenium、chrome、chromedriver
chrome·selenium·测试工具
这里有鱼汤44 分钟前
用卡尔曼滤波器计算个股票相对大盘的相关性——β值
后端·python
m0_558790141 小时前
GPU算力平台评测
服务器·人工智能·python·深度学习·神经网络·目标检测·机器学习
Kyln.Wu1 小时前
【python实用小脚本-169】『Python』所见即所得 Markdown 编辑器:写完即出网页预览——告别“写完→保存→刷新”三连
开发语言·python·编辑器
爱掉发的小李1 小时前
Linux 环境下 Docker 安装与简单使用指南
java·linux·运维·c++·python·docker·php
LetsonH3 小时前
⭐CVPR2025 FreeUV:无真值 3D 人脸纹理重建框架
人工智能·python·深度学习·计算机视觉·3d
蹦蹦跳跳真可爱5893 小时前
Python----大模型(大模型微调--BitFit、Prompt Tuning、P-tuning、Prefix-tuning、LORA)
人工智能·python·深度学习·自然语言处理·transformer
Monkey的自我迭代3 小时前
决策树分类实战:从数据到模型优化
python·决策树·机器学习