Reputation: 1
I have to get the XPATH for dynamic Ids of table. I am trying to get all table ids which have 'sometext' into a list using findElements.
Issue is that they have a similar pattern except for digits in id attribute they vary
<tr>
<td>
<a id="text-132" </a>
</td>
<a id="text-125" </a>
</td>
<td>
<a id="test-122"</a>
</td>
I wrote following code, it doesn't show any error locating xpath but it prints nothing and list size is 0
List<WebElement> my_list = driver.findElements(By.xpath("//*[starts-with(@id, 'text')]"));
System.out.println("Size: " + my_list.size());
for (int i=0; i<my_list.size(); i++) {
System.out.println(my_list.get(i).getText());
}
Upvotes: 0
Views: 913
Reputation: 7339
I would prefer (probably a matter of habbit) - alternative- CSS selectors:
Selector: [attribute*=value]
Example: a[href*="w3schools"]
Description: Selects every element whose href attribute value contains the substring "w3schools"
In your projection:
List<WebElement> myList=driver.findElements(By.cssSelector("a[id*=\"text\"]"));
System.out.println("Size: " + myList.size());
Hope this helps you.
Upvotes: 1