-3

This Xpath works fine when i am passing static id i.e

driver.findElementByXPath("//tr[@id='Billable Client']/td/input").getAttribute("id");

but It doesnt work when I passed variable i.e

String  Level3KeyCase0Trimmed = "Billable Client";
driver.findElementByXPath("//tr[@id='"+Level3KeyCase0Trimmed+"']/td/input").getAttribute("id");

tried by removing '...' single qoutes as well

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • 1
    What do you mean "it doesn't work"? What specific error do you get? – Simon Crane Jan 14 '20 at 14:10
  • In addition to Simon's question, you need to post the code for `findElementByXPath()`. There may be something in there that is causing the problem. Have you tried adding a wait? It could just be a timing issue but it's hard to tell without the error message and more info. – JeffC Jan 14 '20 at 16:18

1 Answers1

0

Within the xpath:

driver.findElementByXPath("//tr[@id='Billable Client']/td/input").getAttribute("id");

If you want the value of the id attribute to be a variable you can use the following solution:

String  Level3KeyCase0Trimmed = "Billable Client";
browser.findElement(By.xpath("//tr[@id='" + Level3KeyCase0Trimmed + "']/td/input")).getAttribute("id");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 4
    How is this any different than the code OP posted? Your XPath is exactly the same. The only thing you changed is `driver` to `browser` (with no explanation as to why and it won't make any difference) and `findElementByXPath` to `findElement` which isn't going to make any difference either. – JeffC Jan 14 '20 at 15:12