如何直接通过 XPath 结果使用 Selenium RC 进行迭代?

发布于 2024-09-05 04:57:22 字数 148 浏览 2 评论 0原文

我目前正在采取首先执行 get_xpath_count 的方法,然后创建一个循环递增索引变量,然后将其注入回原始 xpath 以选择第 n 个结果......毫无疑问,非常尴尬。

有没有一些简单、直接、优雅的方法来直接迭代 xpath 结果?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋意浓 2024-09-12 04:57:22

您的方法可能是实现目标的最简单方法,但是在 Selenium 2 (WebDriver) 中它稍微优雅一些​​。 Java 中的示例如下:

List<WebElement> links = driver.findElements(By.xpath("//a"));
for (WebElement link : links) {
    System.out.println(link.getText());
}

这会将页面上每个链接的链接文本输出到控制台。

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.

左秋 2024-09-12 04:57:22

我们正在使用相同的方法。 :)

所以,如果有更好的解决方案,那么我也想看到它......但我认为不存在。

唉,如果你对这些东西评论得足够好,那就没那么糟糕了......:)

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

橙幽之幻 2024-09-12 04:57:22

我正在做的是在 C# 中使用 HtmlAgility 包并读取完整源代码并应用 xpath,然后返回一个

我可以迭代的节点列表。如果需要,您甚至可以尝试使用 linq。

我很感激我避开了硒,但当时它很方便。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文