0

I have a Element Xpath

//img[@onclick="EditResource(this,'10','0');"]

I need to use this in a loop where i will replace 10 with a variable i.

driver.findElement(By.xpath("//img[@onclick='EditResource(this,'" +  i +"','0');']"))

But it is giving an error No such Element as EditResource is in the double quotes.

1
  • 1
    Please show the HTML document you are selecting from. Commented Jan 13, 2016 at 12:59

3 Answers 3

1

You should not use all the specifics (like the params of the function) from the onclick event, as those may change causing your xpath to fail. You could use the contains() method inside the xpath and this way, even if the parameters change (for any reason), your element is still found:

.//img[contains(@onclick, 'EditResource')]
Sign up to request clarification or add additional context in comments.

Comments

0

You can easily escape the " from your locator like this:

driver.findElement(By.xpath("//img[@onclick=\"EditResource(this,'" +  i +"','0');\"]"))

Comments

0

Simple and easy to look this is...

String str1 = "EditResource(this,'"; String str2 = "','0')']"; driver.findElement(By.xpath(str1+i+str2));

And, you can also do it in one line

driver.findElement(By.xpath("//img[@onclick=\"EditResource(this,'" + i +"','0');\"]"));

\" gives you ' while running the xpath.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.