【问题标题】:Select nth element inside nth element with XPath使用 XPath 选择第 n 个元素内的第 n 个元素
【发布时间】:2021-10-03 03:00:41
【问题描述】:

如何选择第二行的第二个输入?

<div class="row">
  <whatever>
    <input/>
  </whatever>
  <whatever>
    <input/>
  </whatever>
</div>
<div class="row">
  <whatever>
    <input/>
  </whatever>
  <whatever>
    <input/> <!-- select this -->
  </whatever>
</div>

我尝试了(//div[@class='row'])[2](//input)[2],但它不起作用。

我不想要(//div[@class='row'])[2]/whatever[2]/input,因为“whatever”是未知数量的嵌套节点,即它可能是这样的:

<whatever1>
  <whatever2>
    <whatever3>
      <input/>
    </whatever3>
  </whatever2>
</whatever1>

【问题讨论】:

    标签: c# selenium selenium-webdriver xpath


    【解决方案1】:

    您可以使用以下 xpath :

    //div[@class='row'][2]/descendant::input[2]
    

    ((//div[@class='row'])[2]/descendant::input)[2]
    

    说明:

    输入是descendant 不是直接child 所以你不能

    (//div[@class='row'])[2](//input)[2]
    

    【讨论】:

    • 第一个示例使用添加的括号 (//div[@class='row'])[2]/descendant::input[2] 第二个按原样工作。谢谢!
    【解决方案2】:

    实际上,您第一次尝试的固定版本几乎就在这里:

    ((//div[@class='row'])[2]//input)[2]
    

    (//div[@class='row'])[2]//input 匹配所有输入。只需选择第二个匹配项即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      • 2019-09-16
      • 1970-01-01
      • 2015-05-30
      相关资源
      最近更新 更多