在 if 语句中检查 xpath 的值

Check value of an xpath in if statement

您好,我正在尝试检查我的 table 是否使用 IF 语句排序 我正在尝试使用从 xpath 中获取的值,如下所示。

Check if sorted by alphabet
sleep   3s
${stuurman}=    Stuurman
${sorted}=    Get Text    xpath:/html/body/div[1]/div/div[2]/div/div/div/div[1]/div/div[2]/table/tbody/tr[12]/td[2]
IF    ${sorted} == ${stuurman}
    Log To Console  "Sorted"
ELSE
    Log To Console  "Not sorted"
END

但我找到了以下 error:No 关键字,名称为 'Stuurman'。 我在这里做错了什么?

在关键字中设置变量时,您需要使用“设置变量”之类的名称,后跟值。 (右侧应有关键字)

例如

${stuurman}=  Set Variable   Stuurman

在 *** 变量 *** 部分中设置变量时可以省略关键字,例如

*** Variables ***
${stuurman}=   Stuurman

除了 Matthew King 对变量创建的回答之外,您的代码还有一个问题:

IF 中的表达式直接传递给 python 解释器,框架用它们的值替换任何变量。所以

${sorted} == ${stuurman}

被评估为

Stuurman == the text from the UI

现在python中的这个实际上是“变量Stuurman应该等于变量the text from the UI”,但是没有这样的变量,它们还没有被定义py;因此将引发异常。

有两个选项可以使它正确;因为两者都是字符串,您可以用引号将它们括起来,当 RF 将它们替换为值时,它将是一个纯字符串比较:

"${sorted}" == "${stuurman}"     
# will be evaluated as:
# "Stuurman" == "the text from the UI"

另一种选择是使用高级变量语法(我 认为 这是 RF 用户指南中的名称)并且不传递值,而是传递变量本身。这是通过省略大括号,仅在 var 前加上美元符号来完成的。

因此不会发生替换,Python 将获取实际变量并比较它们:

$sorted == $stuurman