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

相关推荐
weixin_4407305019 分钟前
playwright浏览器自动化实战笔记3-登陆以及退出登陆流程-多用户操作
笔记·python·自动化
卷无止境27 分钟前
测试全绿,功能能跑,代码却烂到没法上线:AI编程助手留下的十个坑
后端·python
卷无止境34 分钟前
终端里的AI战争,命令行编程代理全景扫描
python·agent
SamChan9040 分钟前
PDF多语言翻译的格式还原技术拆解:从版面分析到内容重排的工程实现
python·ai·pdf·机器翻译
liuze4081 小时前
安装boss-zhipin-mcp(招聘)
开发语言·python
TickDB1 小时前
Python 接入 A 股盘前集合竞价数据:AkShare 报错到 TickDB 实测的完整解法
python·websocket·行情数据 api
2601_962298271 小时前
【自动化测试】基于Selenium + Python的web自动化框架
自动化测试·python·selenium·测试用例·web框架
2601_962077531 小时前
Python 条件表达式
python·语法·列表推导式·条件表达式·pep308
右耳朵猫AI2 小时前
Python周刊2026W36 | Python 3.15候选版、RISC-V获CPython支持、PEP 843、asyncio修复
python·sqlite·risc-v
liliangcsdn2 小时前
基于信号强度的稀疏选股算法的探索分析
开发语言·python