get element via XPath with a variable in Karate

Viewed 291

I want to get an element with the script() function, but I want to use a variable I have defined within my XPath. Any idea on how this can work?

I'm using Karate 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()

It works fine if I just put the username in, but I want it to be reusable.

Thanks in advance for your input.

1 Answers

you don't need script() to form your dynamic locator,

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