如何使用 XPath 选择任何深度的子元素? [英] How do I select child elements of any depth using XPath?
问题描述
假设我有这个(简化):
Suppose I have this (simplified):
<form id="myform">
<!-- some input fields -->
<input type="submit" value="proceed"/>
</form>
然后我可以通过XPath//form[@id='myform']/input[@type='submit']
选择提交按钮.太好了.
Then I can select the submit button by XPath //form[@id='myform']/input[@type='submit']
. Great.
但是,我的模板可能会更改,我希望在提交按钮所在的深度方面具有灵活性.它可能被放在一张桌子上,像这样:
However, my templates might change and I want to be flexible in the depth in which the submit button is located. It might be put in a table, like this:
<form id="myform">
<!-- some input fields -->
<table><tr><td>
<input type="submit" value="proceed"/>
</td></tr></table>
</form>
我知道我可以选择属于孙的元素,但我不能选择任何深度的 Grand-grand-grand-...-childeren.例如:
I know I can select elements which are grandchildren, but I can't select grand-grand-grand-...-childeren of any depth. E.g.:
//form[@id='myform']/*/input[@type='submit']
只选择孙辈,没有进一步的深度.//form[@id='myform']/*/*/input[@type='submit']
只选择曾孙,没有进一步或更少的深度.//form[@id='myform']/**/input[@type='submit']
无效.
//form[@id='myform']/*/input[@type='submit']
only selects grand-children, no further depths.//form[@id='myform']/*/*/input[@type='submit']
only selects grand-grand-children, no further or less depths.//form[@id='myform']/**/input[@type='submit']
is not valid.
那么,如何在不使用元素 ID 的情况下可靠地选择此提交按钮?
So, how do I select this submit button reliably without using element IDs?
推荐答案
大功告成.只需使用:
//form[@id='myform']//input[@type='submit']
//
快捷方式也可以在表达式中使用.
The //
shortcut can also be used inside an expression.
这篇关于如何使用 XPath 选择任何深度的子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!