Robot Framework - 检查xpath定义的元素是否存在

问题描述 投票:2回答:2

我想知道,我喜欢找或写条件来检查是否存在某些元素。如果它比我想要执行IF条件的身体。如果它不存在而不是执行ELSE的主体。

有这样的条件还是有必要以某种方式自己写?

python testing xpath automated-tests robotframework
2个回答
6
投票

通过使用xpath定位元素,我假设您正在使用Sselenium2Library。在该lib中有一个名为的关键字:

Page Should Contain Element需要一个参数,这是一个selector,例如定义你的元素的xpath。

如果页面不包含指定的元素,则关键字将失败。

对于条件,使用此:

${Result}= Page Should Contain Element ${Xpath} Run Keyword Unless '${RESULT}'=='PASS' Keyword args*

您还可以使用其他关键字:Xpath Should Match X Times


2
投票

我更喜欢使用Get Matching XPath Count,因为如果找不到元素,则接受的答案会抛出错误

${count}= Get Matching XPath Count ${Xpath}
Run Keyword And Return If   ${count} > 0 Keyword args*
© www.soinside.com 2019 - 2024. All rights reserved.