zoukankan      html  css  js  c++  java
  • 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:ProjectRobotdomain>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:ProjectRobotdomainweb.py", line 63, in click_element_screen_shoot
        self.click_element(element)
      File "e:ProjectRobotdomainweb.py", line 53, in click_element
        ac(self.driver).move_to_element(element).click().perform()
      File "E:virtualenvs
    obotlibsite-packagesseleniumwebdrivercommonaction_chains.py", line 80, in perform
        self.w3c_actions.perform()
      File "E:virtualenvs
    obotlibsite-packagesseleniumwebdrivercommonactionsaction_builder.py", line 76, in perform
        self.driver.execute(Command.W3C_ACTIONS, enc)
      File "E:virtualenvs
    obotlibsite-packagesseleniumwebdriver
    emotewebdriver.py", line 321, in execute
        self.error_handler.check_response(response)
      File "E:virtualenvs
    obotlibsite-packagesseleniumwebdriver
    emoteerrorhandler.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)
    

    最后原因是因为刷新页面导致之前拿到的元素对象失效,无法锁定,从而导致改报错。

    坑:

    • 页面点击之后刷新了
    • 之前抓取的元素对象失效
  • 相关阅读:
    jsgraphics插件图形库
    MySQL 基础知识
    第四天:任务开始——网络驱动
    第三天:软件著作权申请
    第二天:办公流程及软件安装
    第一天:products和一些感想
    IMX6Q RTC驱动分析
    PLATFORM设备驱动
    字符设备驱动以及杂项设备驱动
    C程序编译过程及优化选项介绍
  • 原文地址:https://www.cnblogs.com/linga/p/11656045.html
Copyright © 2011-2022 走看看