如何解决硒错误:StaleElementReferenceException
如果你看到附加的图像,我需要翻转那个开关,因为我不希望这个系统以我的名字运行,而是在系统名称下运行。我需要单击此切换开关以切换到以系统模式运行。我收到一个错误 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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。