Tag Archives: selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

After fetching the web page code, because it is under the same Li tag, use one-time fetching, all a tags, and then cycle to do different operations, but throw an exception that cannot find the element.

def office_page(_chrome: Chrome):
    sn = 'Home'
    _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)

Screen log:

(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)

The last reason is that the element object obtained before is invalid and cannot be locked due to refreshing the page, resulting in error correction.

Pit:

The page is refreshed after clicking

The previously captured element object is invalid