Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Java variable to xpath

Is it possible to add a defined Java String variable to an xpath-expression? Here is some code to make it clear:

String newUser = "NewUser123";
driver.findElement(By.xpath("//td[normalize-space(text())=**newUser**]/a")).click();

The xpath should work with the variable newUser, but I don´t know how.

like image 716
Stefan Avatar asked Dec 14 '22 11:12

Stefan


1 Answers

Yes, when you choose Java as your programming language while working with selenium, you can perform all things that are logically possible in Java.

Take your example, xpath is a function which takes string arguments. So Like in Java we concatenate multiple strings using +. you can do the same thing here.

String newUser = "NewUser123";
driver.findElement(By.xpath("//td[normalize-space(text())='"+newUser+"']/a")).click();
like image 86
Nidhi Avatar answered Dec 18 '22 11:12

Nidhi