简体   繁体   English

如何找到跳过内部元素的元素的xpath

[英]how to find xpath of an element skipping the inner elements

I have a complex html structure with lot of tables and divs.. and also the structure might change.我有一个复杂的 html 结构,有很多表格和 div ......而且结构可能会改变。 How to find xpath by skipping the elements in between.如何通过跳过之间的元素来查找 xpath。 for example :例如 :

<table>
  <tr>
    <td>
      <span>First Name</span>
    </td>
    <td>
      <div>
        <table>
          <tbody>
            <tr>
              <td>
                <div>
                  <table>
                    <tbody>
                      <tr>
                        <td>
                          <img src="1401-2ATd8" alt="" align="middle">
                        </td>
                        <td><span><input atabindex="2"  id=
                        "MainLimitLimit" type="text"></span></td>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </td>
  </tr>
</table>

I have to get the input element with respect to the "First Name" span我必须获取关于“名字”跨度的输入元素

eg :例如:

By.xpath("//span[contains(text(), 'First Name')]/../../td[2]/div/table/tbody/tr/td/table/tbody/tr/td[2]/input")

but.. can I skip the between htmls and directly access the input element.. something like?但是..我可以跳过htmls之间并直接访问输入元素..类似的东西吗?

By.xpath("//span[contains(text(), 'First Name')]/../../td[2]//input[contains@id,'MainLimitLimit')]")

您可以使用//这意味着在任何级别

By.xpath("//span[contains(text(), 'First Name')]//td[2]/input[contains@id,'MainLimitLimit')]")

You can try this Xpath :你可以试试这个 Xpath:

//td[contains(span,'First Name')]/following-sibling::td[1]//input[contains(@id, 'MainLimitLimit')]

Explanation :解释 :

select <td><span>First Name</span></td> element :选择<td><span>First Name</span></td>元素:

//td[contains(span,'First Name')]

then get <td> element next to above <td> element :然后在<td>元素上方获取<td>元素:

/following-sibling::td[1]

then get <input> element within <td> element selected in the 2nd step above :然后在上面的第二步中选择的<td>元素中获取<input>元素:

//input[contains(@id, 'MainLimitLimit')]

you can use the "First Name" span as a predicate.您可以使用“名字”范围作为谓词。 Try the code below试试下面的代码

//td[preceding-sibling::td[span[contains(text(), 'First Name')]]]//input[contains(@id,'MainLimitLimit')]

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

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