web-异常:Stale Element Reference Exception

本文探讨了自动化测试中常见的StaleElementReferenceException异常。解释了两种官方给出的原因:元素被完全删除或不再附加到DOM。并通过实际案例分析了如何在JavaScript页面刷新的情况下遇到此异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[自动化测试]Stale Element Reference Exception


关于这个异常,官方给出两个可能原因:

The element has been deleted entirely.The element is no longer attached to the DOM.我的个人理解是,无论是否借助框架,实际上的操作都是先找到这个element,然后对它进行操作(一般就是点击),在这两个动作之前,如果JavaScript把网页给刷新了,那么操作的时候就会碰到Stale Element Reference Exception。所以这个异常并不是每次都会产生,也不是你想复现就能复现。

我自己写的代码 多获取几次这个元素

Traceback (most recent call last): File "C:\Users\czadmin\PyCharmMiscProject\cyllq.py", line 13, in <module> login_button.click() ~~~~~~~~~~~~~~~~~~^^ File "C:\Users\czadmin\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 94, in click self._execute(Command.CLICK_ELEMENT) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\czadmin\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 395, in _execute return self._parent.execute(command, params) ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^ File "C:\Users\czadmin\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 346, in execute self.error_handler.check_response(response) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^ File "C:\Users\czadmin\AppData\Local\Programs\Python\Python313\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: stale element not found (Session info: chrome=134.0.6998.118); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#stale-element-reference-exception Stacktrace: GetHandleVerifier [0x00007FF7552DFE45+26629] (No symbol) [0x00007FF755246010] (No symbol) [0x00007FF7550D931A] (No symbol) [0x00007FF7550EF424] (No symbol) [0x00007FF7550EDEF3] (No symbol) [0x00007FF7550E19A9] (No symbol) [0x00007FF7550DF782] (No symbol) [0x00007FF7550E3A3C] (No symbol) [0x00007FF7550E3B0F] (No symbol) [0x00007FF755131A2D] (No symbol) [0x00007FF755122810] (No symbol) [0x00007FF755157AAA] (No symbol) [0x00007FF755122156] (No symbol) [0x00007FF755157CC0] (No symbol) [0x00007FF755180169] (No symbol) [0x00007FF755157883] (No symbol) [0x00007FF755120550] (No
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_喵酱_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值