HtmlUnit 2.8 getFirstByXPath 与 HtmlUnit 1.14 getFirstByXPath 不同吗?

发布于 2024-11-19 18:32:29 字数 1250 浏览 3 评论 0原文

我有一个看起来像这样的站点结构:

<div class='main_container'>
     <div class='item_container'>
         <div class='body'>
             <span class='item_name'>Item 1</span>
             <span class='item_desc'>Desc 1</span>
         </div>
     </div>
     <div class='item_container'>
         <div class='body'>
             <span class='item_name'>Item 2</span>
             <span class='item_desc'>Desc 2</span>
         </div>
     </div>
     ...
</div><!--End of main_container--> 
//Note: Some divs might not have <span @class='item_name'>Item N</span> or other elements inside the item_container

在 HtmlUnit 1.14 中,如果我想获取所有项目名称:

List<HtmlDivision> divs = (List<HtmlDivision>)page.getByXPath("//div[@class='item_container']");
for(HtmlDivision div:divs){
    String name = ((HtmlElement)div.getFirstByXPath("//span[@class='item_name']")).asText();
    System.out.println(name);
}

输出:

Item 1
Item 2
...

但在 HtmlUnit 2.8 中,当我执行相同操作时,我得到了。

Item 1
Item 1
...

HtmlUnit 2.8 中有解决方法吗?

I have a site structure that looks something like this:

<div class='main_container'>
     <div class='item_container'>
         <div class='body'>
             <span class='item_name'>Item 1</span>
             <span class='item_desc'>Desc 1</span>
         </div>
     </div>
     <div class='item_container'>
         <div class='body'>
             <span class='item_name'>Item 2</span>
             <span class='item_desc'>Desc 2</span>
         </div>
     </div>
     ...
</div><!--End of main_container--> 
//Note: Some divs might not have <span @class='item_name'>Item N</span> or other elements inside the item_container

In HtmlUnit 1.14 if I want to get all item name:

List<HtmlDivision> divs = (List<HtmlDivision>)page.getByXPath("//div[@class='item_container']");
for(HtmlDivision div:divs){
    String name = ((HtmlElement)div.getFirstByXPath("//span[@class='item_name']")).asText();
    System.out.println(name);
}

Output:

Item 1
Item 2
...

But in HtmlUnit 2.8 when I do the same I got.

Item 1
Item 1
...

Is there a workaround on this in HtmlUnit 2.8?

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

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

发布评论

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

评论(1

儭儭莪哋寶赑 2024-11-26 18:32:29

可能 HtmlUnit 1.4 有一个您正在利用/依赖的错误。

在您展示的代码中,for 循环内部的 XPath 每次执行时都应返回相同的元素(如 v2.8 中所示),因为它以 //< 开头/code>,从根节点开始遍历整个文档并返回它所找到的第一个发现。

如果您希望它与循环中的

相关,则应将 XPath 调整为:.//span[@class='item_name']

It may be that HtmlUnit 1.4 had a bug that you were exploiting/relying on.

In the code that you showed, the XPath inside of the for loop should return the same element each time it executes(as it does in v2.8), because it starts with //, which looks through the entire document starting at the root node and returns the first one that it finds.

If you want it to be relative from the <div> in the loop, you should adjust your XPath to: .//span[@class='item_name']

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