In my page I have some ids like "foo:searchResults12345" that are composed of a static part ("foo:searchResults" and a dynamic rendered one - one or more numbers. I want to retrieve every id that contains the static part "foo:searchResults", ignoring the dynamic one. I am thinking at using regex for the pattern text, but the problem is that I cannot find any method in Selenium Api to help me with this, such as selenium.getAllIdsInPage(). Anybody had the same problem and figured out a solution?
+1
A:
I would recommend combining getXpathCount
with a loop to iterate through each match. For example the following xpath should return the total number of matching nodes when used with getXpathCount
//*[starts-with(@id, 'foo:searchResults')]
You should then be able to loop through each match using the following example XPath:
/descendant::*[starts-with(@id, 'foo:searchResults')][1]
Where 1 would be replaced by the current count in the loop.
Below is an example in Java that should print the matching IDs to system.out:
@Test
public void outputDynamicIds() {
selenium.open("http://www.example.com/");
int elementCount = selenium.getXpathCount("//*[starts-with(@id, 'foo:searchResults')]").intValue();
for (int i = 1; i < elementCount+1; i++) {
System.out.println(selenium.getAttribute("/descendant::*[starts-with(@id, 'foo:searchResults')][" + i + "]@id"));
}
}
Dave Hunt
2009-11-23 22:08:23
Thank you very much. I eventually combined a couple of xpath methods as: matches, contains and I succeeded in having an approximate solution for the issue.Anyway, your approach is interesting: until now I didn't know about this xpath formula: /descendant::*[starts-with(@id, 'foo:searchResults')][" + i + "]@id and I guess it can be of great help. Thx again.
ratzusca
2009-11-24 14:11:54