根据元素的存在运行机器人关键字

zor*_*b76 2 automated-tests robotframework selenium2library

有没有办法获取有关机器人框架中页面上是否存在元素的信息?

如果页面上存在元素,我想采取一项行动,如果元素不存在,我想采取另一种行动。

在Selenium中,我将使用findElements(),这将返回元素列表,如果未找到任何内容,则返回一个空列表。

但是,在Robot中,如果我使用却Get webelements什么也没找到,则测试因错误而中断:

ValueError: Element locator 'id=asdf' did not match any elements.
Run Code Online (Sandbox Code Playgroud)

Tod*_*kov 5

一种方法是获取布尔值是页面中的元素:

${present}=    Run Keyword And Return Status    Page Should Contain Element   ${your_locator}
Run Code Online (Sandbox Code Playgroud)

,然后根据该值控制流:

Run Keyword If    ${present}    The keyword(s) if present
                  ...  ELSE     The keyword(s) if NOT present
Run Code Online (Sandbox Code Playgroud)

相反,Page Should Contain Element您可以使用Element Should Be Visible