selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常。
def office_page(_chrome: Chrome): sn = '官网首页' _xpath = '//li[@class="scNav_Item"]/a' sc_nav_list = _chrome.driver.find_elements_by_xpath('//li[@class="scNav_Item"]/a') print(len(sc_nav_list)) for item in sc_nav_list: print(item) png = _chrome.click_element_screen_shoot(item) save_picture(sn, png) time.sleep(1) png = _chrome.pull_down_screen_shoot() save_picture(sn, png)
屏幕日志:
(robot) E:\Project\Robot\domain>E:/virtualenvs/robot/Scripts/python.exe e:/Project/Robot/domain/run.py DevTools listening on ws://127.0.0.1:55976/devtools/browser/e36c0dea-4bd8-4f49-b910-812e5f5542fa 5 <selenium.webdriver.remote.webelement.WebElement (session="3bc66d5ecc5f15a0fc6f125f6ec323e7", element="93079042-927d-49af-8424-44979cacf9cc")> <selenium.webdriver.remote.webelement.WebElement (session="3bc66d5ecc5f15a0fc6f125f6ec323e7", element="e330b1bf-b80e-4c7c-a6f9-9f33ffc6c721")> Traceback (most recent call last): File "e:/Project/Robot/domain/run.py", line 111, in <module> run(url) File "e:/Project/Robot/domain/run.py", line 78, in run office_page(chrome) File "e:/Project/Robot/domain/run.py", line 60, in office_page png = _chrome.click_element_screen_shoot(item) File "e:\Project\Robot\domain\web.py", line 63, in click_element_screen_shoot self.click_element(element) File "e:\Project\Robot\domain\web.py", line 53, in click_element ac(self.driver).move_to_element(element).click().perform() File "E:\virtualenvs\robot\lib\site-packages\selenium\webdriver\common\action_chains.py", line 80, in perform self.w3c_actions.perform() File "E:\virtualenvs\robot\lib\site-packages\selenium\webdriver\common\actions\action_builder.py", line 76, in perform self.driver.execute(Command.W3C_ACTIONS, enc) File "E:\virtualenvs\robot\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "E:\virtualenvs\robot\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document (Session info: chrome=77.0.3865.90)
最后原因是因为刷新页面导致之前拿到的元素对象失效,无法锁定,从而导致改报错。
坑:
- 页面点击之后刷新了
- 之前抓取的元素对象失效
作者: 咕咚!
出处: https://www.cnblogs.com/linga/
关于作者:专注虚拟化,运维开发,RPA,Rust,Go,Python!
本文版权归作者和博客园共有,禁止*.csdn.net转载,禁止以盈利为目的的转载,转载文章,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(oldsixa@163.com)咨询.
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步