如何找到跳过内部元素的元素的xpath [英] how to find xpath of an element skipping the inner elements

查看:38
本文介绍了如何找到跳过内部元素的元素的xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的 html 结构,里面有很多表格和 div.. 而且结构可能会改变.如何通过跳过之间的元素来查找 xpath.例如:

<tr><td><span>名字</span></td><td><div><表格><tr><td><div><表格><tr><td><img src="1401-2ATd8" alt="" align="middle"></td><td><span><input atabindex="2" id="MainLimitLimit" type="text"></span></td></tr></tbody>

</td></tr></tbody>

</td></tr>

我必须获取与名字"跨度相关的输入元素

例如:

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

但是.. 我可以跳过 html 之间的内容并直接访问输入元素.. 类似的东西吗?

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

解决方案

你可以试试这个 Xpath :

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

说明:

选择名字元素:

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

然后获取元素旁边的元素:

/following-sibling::td[1]

然后在上面的第二步中选择的 元素中获取 元素:

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

I have a complex html structure with lot of tables and divs.. and also the structure might change. How to find xpath by skipping the elements in between. 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?

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

解决方案

You can try this Xpath :

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

Explanation :

select <td><span>First Name</span></td> element :

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

then get <td> element next to above <td> element :

/following-sibling::td[1]

then get <input> element within <td> element selected in the 2nd step above :

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

这篇关于如何找到跳过内部元素的元素的xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆