如何找到跳过内部元素的元素的xpath [英] how to find xpath of an element skipping the inner elements
问题描述
我有一个复杂的 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')]
然后获取 然后在上面的第二步中选择的 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 : I have to get the input element with respect to the "First Name" span eg : but.. can I skip the between htmls and directly access the input element.. something like?
You can try this Xpath : Explanation : select then get then get
这篇关于如何找到跳过内部元素的元素的xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!元素旁边的 元素: /following-sibling::td[1]
元素中获取 元素:
//input[contains(@id, 'MainLimitLimit')]
<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>
By.xpath("//span[contains(text(), 'First Name')]/../../td[2]/div/table/tbody/tr/td/table/tbody/tr/td[2]/input")
By.xpath("//span[contains(text(), 'First Name')]/../../td[2]//input[contains@id,'MainLimitLimit')]")
//td[contains(span,'First Name')]/following-sibling::td[1]//input[contains(@id, 'MainLimitLimit')]
<td><span>First Name</span></td>
element ://td[contains(span,'First Name')]
<td>
element next to above <td>
element :/following-sibling::td[1]
<input>
element within <td>
element selected in the 2nd step above ://input[contains(@id, 'MainLimitLimit')]
登录
关闭