You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行Get Text命令时遭遇StaleElementReferenceException问题求助

解决StaleElementReferenceException并获取目标元素文本

现有相关信息

1. 原始Get Text关键字代码

Get Text
[Arguments] ${locator}
Wait For Condition return document.readyState=="complete"
Wait Until Page Contains Element ${locator}
Wait Until Keyword Succeeds 30 30 Identify Element ${locator}
Wait Until Element Is Visible ${locator}
Sleep 2s
${text} SeleniumLibrary.Get Text ${locator}
[Return] ${text} 

2. 调用关键字的代码

${Received Amount}= Get Text (//*[@id="fxConfirmModalHeaderFx"]/div[2]/div[1]/section/div[2]/div/div[14]/span/span[1]) | (//*[@id="fxConfirmModalHeaderFx"]/div[2]/div[1]/section/div[3]/div/div[14]/span/span[1])
Log to console Received Amount:${Received Amount}

3. 目标元素XPath

(//*[@id="fxConfirmModalHeaderFx"]/div[2]/div[1]/section/div[2]/div/div[14]/span/span[1]) | (//*[@id="fxConfirmModalHeaderFx"]/div[2]/div[1]/section/div[3]/div/div[14]/span/span[1])

4. 目标元素HTML结构

<div _ngcontent-pfc-c193="" class="A12 reAmtDetail"><span _ngcontent-pfc-c193=""><span class="sr-only">700.00 US dollars</span><span aria-hidden="true">$700.00</span><span aria-hidden="true" class="currency-inline"> USD</span></span></div>

5. 报错信息

StaleElementReferenceException: Message: stale element reference: stale element not found (Session info: chrome=120.0.6099.225)

6. 尝试的Get Text2关键字(返回空值)

Get Text2 
[Arguments] ${locator}
Wait For Condition return document.readyState=="complete"
Wait Until Page Contains Element ${locator}
Wait Until Keyword Succeeds 5 5 Identify Element ${locator}
Wait Until Element Is Visible ${locator}
Sleep 2s
${text}= Set Variable ${EMPTY}
:FOR ${INDEX} IN RANGE 3
\ Run Keyword And Ignore Error ${text}= SeleniumLibrary.Get Text ${locator}
\ Exit For Loop If '${text}' != '${EMPTY}' # Break the loop if text is retrieved
\ Sleep 2s # Wait before retrying
[Return] ${text} 

解决方案

1. 核心问题分析

StaleElementReferenceException是因为元素已被DOM重新渲染或移除,之前缓存的元素引用失效。你之前的重试逻辑仅重复调用Get Text,没有重新定位元素,导致复用失效引用返回空值。

2. 优化后的Get Text关键字

用Wait Until Keyword Succeeds包裹完整的定位+获取文本流程,确保每次失败后重新定位元素:

Get Text Fixed
[Arguments] ${locator}
# 最多重试30次,间隔1秒,失败自动重新执行核心逻辑
${text}= Wait Until Keyword Succeeds 30 1
...    Run Keyword
...        Wait For Condition return document.readyState=="complete"
...        Wait Until Element Is Visible ${locator}
...        ${temp_text}= SeleniumLibrary.Get Text ${locator}
...        Return From Keyword If '${temp_text}' != '${EMPTY}' ${temp_text}
[Return] ${text}

3. 优化XPath定位

原XPath用|选择两个元素,易受页面结构变动影响。结合HTML结构,改用更稳定的定位:

  • 直接定位金额元素://div[contains(@class,'reAmtDetail')]/span/span[@aria-hidden='true' and not(contains(@class,'currency-inline'))]
  • 或定位父容器获取完整金额文本://div[contains(@class,'reAmtDetail')]/span

调用示例:

${Received Amount}= Get Text Fixed //div[contains(@class,'reAmtDetail')]/span/span[@aria-hidden='true' and not(contains(@class,'currency-inline'))]
Log to console Received Amount:${Received Amount}

4. 额外优化点

  • 移除固定Sleep,用SeleniumLibrary的等待关键字替代,避免低效或不稳定。
  • 用Wait Until Element Is Visible替代Wait Until Page Contains Element,确保元素可见后再操作。
  • 若为Angular应用,可添加Wait For Angular关键字确保页面渲染完成。

内容的提问来源于stack exchange,提问作者SavannahCat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 20:05:59