Java selenium xpath - 获取特定元素下的所有元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27617510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java selenium xpath - getting all elements under a specific element
提问by Hrvoje85
this is a simplified HTML structure i'm searching through:
这是我正在搜索的简化 HTML 结构:
<div class="main">
...other stuff...
<td class="child">44</td>
<td class="child">59</td>
<td class="child">11</td>
</div>
<div class="main">
...other stuff...
<td class="child">5</td>
<td class="child">14</td>
<td class="child">98</td>
</div>
...this kind of structure repeats with similar numbers a few more times but with identical class names
I need to extract all the numbers under the first found main class so I've made a query to search for the first main, and all td's with the specific class under it. Can somebody give me a hint what I'm doing wrong since this query gives me all the numbers from all td's with class "child" in all "main" div's:
我需要提取第一个找到的主类下的所有数字,所以我做了一个查询来搜索第一个主类,以及它下面的特定类的所有 td。有人可以给我一个提示,我做错了什么,因为这个查询给了我所有“主”div中所有带有“child”类的td的所有数字:
List<WebElement> koefi = driver.findElements(By.xpath("//div[@class='main'][1]//td[@class='child']"));
What am I doing wrong or is my logic right but I'm missing some other parts of html which I haven't pasted here since the structure is too cumbersome..?
我做错了什么,或者我的逻辑是否正确,但我错过了 html 的一些其他部分,由于结构太麻烦,我没有在这里粘贴..?
Thank You!!
谢谢!!
p.s.: I tried this also but again, I get contents of all td's with "child" class, and not only the first "main"..
ps:我也试过这个,但再次,我用“子”类获得了所有 td 的内容,而不仅仅是第一个“主”..
List<WebElement> koefi = driver.findElements(By.xpath("//*[1][@class='main']//td[@class='child']"));
UPDATE:I managed to solve my problem by first getting the first occurence of the "main" div which is by default found by the .findElement function:
更新:我设法解决了我的问题,首先获得了“主”div 的第一次出现,这是默认情况下由 .findElement 函数找到的:
WebElement element = driver.findElement(By.xpath("//*[1][@id='main']"));
And then extracting with .findElements function the "child" classes:
然后使用 .findElements 函数提取“子”类:
List<WebElement> kk = element.findElements(By.className("child"));
I am still unable to figure out why doesn't the .findElements with my xpath work, or it works too well, it extracts every "main" class and not only the first one. And the original HTML is too big to paste here, so I don't want to bother you guys!!
我仍然无法弄清楚为什么我的 xpath 中的 .findElements 不起作用,或者它工作得很好,它提取了每个“主”类,而不仅仅是第一个。而且原来的HTML太大了,不能贴在这里,所以我不想打扰你们!!
采纳答案by SiKing
A much cleaner solution would be to first grab all the div
s with class main
, like so:
一个更简洁的解决方案是首先div
使用 class获取所有s main
,如下所示:
List<WebElement> allDivs = driver.findElements(By.className("main"));
Then, as you specified, find all the td
s with class child
, like so:
然后,按照您的指定,找到所有td
带有 class的s child
,如下所示:
List<WebElement> tds = allDivs[0].findElements(By.className("child"));
After that, it is just a matter of iterating over all the "tds" and read out your values.
之后,只需遍历所有“tds”并读出您的值即可。
回答by Ian Roberts
You say in a comment that
你在评论中说
the "main"'s are not direct siblings
“主要”不是直接的兄弟姐妹
so I suspect you are falling foul of a common error related to the definition of //
in XPath. The path
所以我怀疑你犯了一个与//
XPath 中的定义相关的常见错误。路径
//div[@class='main'][1]
does notselect the first "main" div in the document. The reason for this is that //
is a shorthand for /descendant-or-self::node()/
(including the leading and trailing slashes), so what this path actually means is
并没有选择文档中的第一个“主”分区。这样做的原因是这是(包括前导和尾随斜线)//
的简写/descendant-or-self::node()/
,所以这条路径的实际含义是
/descendant-or-self::node()/child::div[@class='main'][1]
When you see it fully expanded you realise that the [1]
relates to the child::
step and not the search for descendants, i.e. you'll get all the div
elements in the document that have the class "main" andare the first div-with-class-main under their respective parent elements. If your actual HTML is
当你看到它完全展开你意识到[1]
涉及child::
的步骤,而不是为了子孙的搜索,即你会得到所有的div
文档中有类“主要”的元素和是第一个div与-类主在它们各自的父元素下。如果您的实际 HTML 是
<div>
<div class="main">...</div>
</div>
<div>
<div class="main">...</div>
</div>
then that XPath would select both of them (they're both the first under their parents). If you do just want the first one in the document then you should use the descendant::
axis
那么 XPath 会选择他们两个(他们都是他们父母的第一个)。如果您只想要文档中的第一个,那么您应该使用descendant::
轴
/descendant::div[@class='main'][1]
which will give you the first matching descendant only.
这只会给你第一个匹配的后代。