Selenium 错误:StaleElementReferenceException

Selenium Error: StaleElementReferenceException

提问人:Shiv Patel 提问时间:1/22/2021 最后编辑:Shiv Patel 更新时间:1/22/2021 访问量:113

问:

如果您看到附加的图像,我需要拨动该开关,因为我不希望这个系统以我的名义运行,而是以系统名称运行。我需要单击此切换开关以切换为系统模式运行。我收到一个错误 StaleElementReferenceException,当我做一些研究时,我需要导入 NoSuchElementException 和 StaleElementReferenceException 并将其分配给igonered_exception然后调用此变量在 run_as_button 变量的 webdriverwait 函数中初始化它......当我这样做时,我仍然收到错误。

图像

您将在下面找到 python 中的相关代码,其中包括导致此错误的导入和代码行

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.support import expected_conditions as EC

#ignore exception
igonered_exception = (NoSuchElementException, StaleElementReferenceException)

#run as system
run_as_button = WebDriverWait(driver,120,ignored_exceptions=igonered_exception).until(EC.element_to_be_clickable((By.XPATH,('//*[@id="core-page main"]/div[2]/div/lp-section/section/div/div/div[2]/div[3]/lp-slide-toggle/div'))))
action.move_to_element(run_as_button).click(run_as_button).perform()

下面是拨动开关的 HTML 代码的截图

<div _ngcontent-qyq-c18="" class="row">
   <div _ngcontent-qyq-c18="" class="col col-md-3"></div>
   <div _ngcontent-qyq-c18="" class="col-12 col-md-2 d-flex align-items-center"><label _ngcontent-qyq-c18="">Run as System</label></div>
   <div _ngcontent-qyq-c18="" class="col-12 col-md-4">
      <lp-slide-toggle _ngcontent-qyq-c18="" _nghost-qyq-c10="">
         <div _ngcontent-qyq-c10="" class="slide-toggle">
            <input _ngcontent-qyq-c10="" type="checkbox" id="kl-toggle-1611252633257330581">
            <label _ngcontent-qyq-c10="" for="kl-toggle-1611252633257330581">
               <div _ngcontent-qyq-c10="">
                  <!---->
               </div>
               <div _ngcontent-qyq-c10="">
                  <!---->
               </div>
            </label>
         </div>
      </lp-slide-toggle>
   </div>
   <div _ngcontent-qyq-c18="" class="col col-md-3"></div>
</div>

您将在下面找到错误消息的完整输出

Traceback (most recent call last):
  File "c:/Users/patel/Desktop/Python/Automation/import_file.py", line 59, in <module>
    action.move_to_element(run_as_button).click(run_as_button).perform()
  File "C:\Anaconda\lib\site-packages\selenium\webdriver\common\action_chains.py", line 80, in perform
    self.w3c_actions.perform()
  File "C:\Anaconda\lib\site-packages\selenium\webdriver\common\actions\action_builder.py", line 76, in perform
    self.driver.execute(Command.W3C_ACTIONS, enc)
  File "C:\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Anaconda\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=87.0.4280.141)
python-3.x selenium selenium-webdriver chrome-web-driver

评论

0赞 undetected Selenium 1/22/2021
这听起来像是一个 X-Y 问题。与其寻求帮助解决问题,不如编辑您的问题并询问实际问题。你想做什么?
0赞 pcalkins 1/22/2021
“lp-slide-toggle”是无效的 HTML...这意味着它被用作占位符,一旦脚本命中它,它就会被替换。这会导致过时的元素异常。加载页面,然后检查要定位的元素。(右键单击,选择“检查”)它还有助于在手动单击时观察检查器......处理程序附加到的元素应该闪烁...此外,无需添加忽略的异常...WebDriverWait 已经包含了正确的。
0赞 Shiv Patel 1/22/2021
@pcalkins所以我将该行更新到下面,但我仍然得到陈旧的元素异常 run_as_button = WebDriverWait(driver, 120).until(EC.element_to_be_clickable((By.XPATH,('//*[@id=“core-page main”]/div[2]/div/lp-section/section/div/div/div/div[2]/div[3]'))))
0赞 pcalkins 1/22/2021
“lp-section”也是一个占位符......检查您的目标并在此处发布该标记。(右键单击要复制到剪贴板的节点)您应该真正简化您的 XPath 以定位您想要的内容。(虽然这不会导致你在这里的特殊问题,但基于路径的 xpath 是“脆弱的”.. 相反,如果可用,请使用标签的 “id”。 例如: //input[@id='kl-toggle-1611252633257330581']
0赞 pcalkins 1/22/2021
需要明确的是,您的 WebdDriverWait 只在等待它找到这个特定的标签......这是一个占位符,稍后将通过 JavaScript 编写。(该页面可能是延迟加载或类似的东西......然后,在您调用该元素引用的操作之前,它会被覆盖,因此它是“过时的”。(它不再存在,或者 DOM 已更新,因此指针指向不正确)

答: 暂无答案