【问题标题】:Robot Framework - check if element defined by xpath existsRobot Framework - 检查 xpath 定义的元素是否存在
【发布时间】:2017-02-21 04:06:45
【问题描述】:

我想知道,我很想查找或编写条件来检查某些元素是否存在。如果确实如此,那么我想执行 IF 条件的主体。如果不存在则执行 ELSE 的主体。

有没有这样的条件,还是有必要自己写?

【问题讨论】:

  • 您可以使用Get Matching XPath CountGet webelementGet webelements的结果编写条件语句

标签: python testing xpath automated-tests robotframework


【解决方案1】:

通过使用 xpath 定位元素,我假设您使用的是Sselenium2Library。在该库中有一个名为:

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

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

对于条件,使用这个:

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

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

【讨论】:

  • 非常感谢,我是 Selenium 的新手,所以这很有帮助!
  • 只有在我这样尝试时才对我有用:${Result}= Run Keyword And Return Status Page Should Contain Element ${Xpath} Run Keyword Unless ${RESULT} 结果应该是 False 或 True
  • 记得在关键字和等号之间留一个以上的空格
【解决方案2】:

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

${count}= Get Matching XPath Count ${Xpath}
Run Keyword And Return If   ${count} > 0 Keyword args*

【讨论】:

    猜你喜欢
    • 2021-03-27
    • 2016-09-30
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 2021-09-14
    • 2011-08-07
    相关资源
    最近更新 更多