在 Karate 中使用变量通过 XPath 获取元素

get element via XPath with a variable in Karate

我想用 script() 函数获取一个元素,但我想使用我在 XPath 中定义的变量。知道这是如何工作的吗?

我正在使用空手道 1.0.1。

  * def username = __arg.username
  * print 'username:', username
  * def listRowElement = script("//div[@aria-colindex='1' and text()='"+username+"']", '_.textContent')
  * mouse().move(listRowElement).click()

如果我只输入用户名就可以正常工作,但我希望它可以重复使用。

提前感谢您的意见。

您不需要script()来形成您的动态定位器,

* def listRowElement = "//div[@aria-colindex='1' and text()='" + username + "']"
* mouse().move(listRowElement).click()