views:

156

answers:

4

I'm currently resorting to first doing a get_xpath_count, and then creating a loop incrementing an index variable, 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!

A: 

Hi.

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... :)

Hannibal
A: 

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.

danswain
+1  A: 

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.

Dave Hunt
A: 

Dave: Yes, but if you don't want to use WebDriver, then what option have you left. I want to use selenium, not webDriver since my initial test is using it. So i don't / can't switch to webDriver.

Hannibal
Selenium 2 uses the WebDriver API, and I did say that in Selenium 1 you probably already have the most suitable solution.
Dave Hunt
Hmmm.. so if you started your test using selenium, like:selenium.start();selenium.click("somebutton");Then how do you switch over to using webDriver?thanks, for the answer.
Hannibal