我在“获取文本”命令上遇到过时元素异常。
这是代码:
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}
然后,进一步传递此函数来填充电子表格:
${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}
这是查找该字段的 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])
以及页面上元素的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>
错误如下:
StaleElementReferenceException: Message: stale element reference: stale element not found (Session info: chrome=120.0.6099.225)
我确实尝试了 try catch,但是当我这样做时,没有数据被传递,所有字段都是空白的。
这是代码:
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}
在获取元素的文本之前,请添加 Reload Page 作为关键字。