0

I am trying to grab a dynamic element with an incremental number in the XPath.

Here is the HTML of the element that I am trying to find:

<a class="delete_link" data-hide-id="list_169" data-delete-action="/panel/menuitem/menudelete/id/169" href="#">Delete</a>

Here is my code:

driver.findElement(By.xpath("//*[@id=list_"+pageID+"")).click();

where pageID is the string number following "list_"

I cannot seem to get the syntax right for it to find this element. I would normally just find it by ID, but this element doesn't have a normal ID identifier as you can see.

1 Answer 1

1

You were using the wrong attribute name @id instead of @data-hide-id.
Try the following XPath expression:

"//a[@data-hide-id = 'list_" + pageID + "']"

In a whole command this could look like

driver.findElement(By.xpath("//a[@data-hide-id = 'list_" + pageID + "']")).click();
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this, but it seems to be missing one quotation as there are 7 in the whole string, and doesnt like that pageID is a string instead of an int I had to make pageID a string because of how I pull it to account for the dynamic element. Here is the code for that: WebElement activeName = driver.findElement(By.xpath("//input[contains(@name,'link-active')]")); String pageName = activeName.getAttribute("name"); String pageID = pageName.replaceAll("[^0-9]", ""); System.out.println(pageID); Sorry I dont know how to block code in comments
Thanks for your comment. I changed the quotes including a single quote '. Now there are even counts of quotes in the expression.
Thanks! That works. So the double quotes go around the inserted object within the xpath value single quotes. Got it! Much love dude

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.