获取相对于导航器对象的特定后代的最佳方法
Best way to get a specific descendant relative to a Navigator Object
这是我正在使用的 HTML 的示例:
<div //this is the Navigator I have
<div
<div class = 'myClass'
<div
<sort-table
<table
<thead </thead>
<tbody </tbody> //this is the Navigator I want
</table>
</sort-table>
</div>
</div>
... (you get the idea)
我有一个方法可以获取相对于特定导航器的 table。有问题的导航器链接到我在上面的 HTML 中标记的 div。我想要它 return tbody。
我很惊讶 Navigator 没有 getAllDecendents 方法。
现在我的方法是这样的:
Navigator getTable(Navigator config){
return config.children($(By.xpath("//div[@class='myClass']"))).children().children().children().children($(By.xpath("//tbody")))
}
它工作正常,但我不喜欢我必须执行 children() 链。我不得不想象有一种方法可以根据特定的选择器获取后代,但我在 API
中找不到类似的东西
我应该补充一点,我不只是使用 xpath 来抓取 table 是有充分理由的。这只是 HTML 的一小段。同样的结构在页面上重复了很多次,这就是我使用 getTable 方法的原因。
也许您想使用 .find() 而不是 .children()?
$("div.myClass").find("tbody")
这是我正在使用的 HTML 的示例:
<div //this is the Navigator I have
<div
<div class = 'myClass'
<div
<sort-table
<table
<thead </thead>
<tbody </tbody> //this is the Navigator I want
</table>
</sort-table>
</div>
</div>
... (you get the idea)
我有一个方法可以获取相对于特定导航器的 table。有问题的导航器链接到我在上面的 HTML 中标记的 div。我想要它 return tbody。
我很惊讶 Navigator 没有 getAllDecendents 方法。
现在我的方法是这样的:
Navigator getTable(Navigator config){
return config.children($(By.xpath("//div[@class='myClass']"))).children().children().children().children($(By.xpath("//tbody")))
}
它工作正常,但我不喜欢我必须执行 children() 链。我不得不想象有一种方法可以根据特定的选择器获取后代,但我在 API
中找不到类似的东西我应该补充一点,我不只是使用 xpath 来抓取 table 是有充分理由的。这只是 HTML 的一小段。同样的结构在页面上重复了很多次,这就是我使用 getTable 方法的原因。
也许您想使用 .find() 而不是 .children()?
$("div.myClass").find("tbody")