How to iterate with Selenium RC over XPath results directly?
I'm currently resorting to first doing a get_xpath_count, and then creating a loop incrementing an index v开发者_C百科ariable, which in turn I inject back into the original xpath to select the nth result... very awkward no doubt.
Is there some simple, direct, elegant way to iterate directly over the xpath results?
Thanks!
Your approach is probably the simplest way of achieving your goal, however it's slightly more elegant in Selenium 2 (WebDriver). An example in Java is below:
List<WebElement> links = driver.findElements(By.xpath("//a"));
for (WebElement link : links) {
System.out.println(link.getText());
}
This would output the link text for every link on the page to the console.
We are using the same approach. :)
So if there is a better solution then i'd like to see it too... But i don't think there is.
Alas, if you comment the stuff well enough it's not that bad... :)
What I'm doing is to use the HtmlAgility pack in c# and read in the full source and apply the xpath which then returns a nodelist
I can iterrate over. You can even try using linq if you want.
I appreciate I'm sidestepping selenium but it was convenient at the time.
精彩评论