简体   繁体   English

试图获取最后一个 TABLE 元素(xpath)

[英]Trying to get the last TABLE element (xpath)

I've got an html document and am trying to access the last TABLE element.我有一个 html 文档,正在尝试访问最后一个 TABLE 元素。

If I do the following - I get the correct snapshot length of 5.如果我执行以下操作 - 我会得到正确的快照长度 5。

var headings = document.evaluate( ".//table[@class='blue']",document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);

Looking at the HTML page structure - 2 of the TABLES captured by the xpath are nested查看 HTML 页面结构 - xpath 捕获的 2 个 TABLES 嵌套

TABLE TABLE TABLE TABLE TABLE << this is what I'm interested in.表表表表表<<这是我感兴趣的。

There are only 5 tables in the document with class='blue'文档中只有 5 个表,class='blue'

I'd like access to the last one - so I did:我想访问最后一个 - 所以我做了:

var headings = document.evaluate( ".//table[@class='blue'][last()]",document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);

It returns an unexpected length of 4 - I was expecting 1.它返回一个意外的长度 4 - 我期待 1。

The last snapshot entry (idx=3) is the correct TABLE.最后一个快照条目 (idx=3) 是正确的 TABLE。 With last() in, it seems to be skipping 1 table that's hidden.使用 last() 时,它似乎跳过了 1 个隐藏的表。

How can I just get the last TABLE element?我怎样才能获得最后一个 TABLE 元素?

Looking at the html document - there are 5 tables with class='blue' and two of them are nested查看 html 文档 - 有 5 个带有 class='blue' 的表,其中两个是嵌套的

TABLE
TABLE
  TABLE
  TABLE
TABLE   <<-- I'm interested in this one

I tried我试过

var headings = document.evaluate( ".//table[@class='blue' and position() = last()]",document.body, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);

and it returned a length of 3 - the middle three tables.它返回的长度为 3 - 中间的三个表。

Looks like the correct answer is看起来正确的答案是

"(.//table[@class='blue'])[last()]"

needed the perens around table .需要围绕佩伦斯table

Is there any inefficiencies?有没有效率低下的地方?

Better way to do the same?更好的方法来做同样的事情?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM