xpath with following-sibling
我想访问包含文本'未来20天的OODR项目'的class =='box-title'类名为'table table-hover'的表。任何人都可以帮助我得到这个xpath吗?我试着跟随兄弟姐妹,但没有运气。提前致谢。xpath with following-sibling
<?xml version="1.0" encoding="UTF-8"?>
<div id="topTenSellers" class="box box-solid box-primary frontpageWidget">
<div class="box-header">
<i class="fa fa-group" />
<h3 class="box-title">OODR Items for next 20 Days</h3>
<div class="box-tools pull-right">
<button class="btn btn-primary btn-sm" data-widget="collapse">
<i class="fa fa-minus" />
</button>
</div>
</div>
<!-- /.box-header -->
<div class="box-body no-padding">
<table class="table table-hover">
<tbody>
<tr>
<th style="width: 10px">#</th>
<th>Booking</th>
<th>Item Start Date</th>
<th>Site</th>
<th>Supplier</th>
</tr>
<tr>
<td>
<strong>1</strong>
</td>
<td>
<a href="" target="_blank">(642143)</a>
</td>
<td>21/10/2017 00:00:00</td>
<td>Ski</td>
<td>OODR - Out of Date Range</td>
</tr>
</tbody>
</table>
</div>
<!-- /.box-body -->
</div>
你可以试试following
代替following-sibling
提到h3
和table
节点没有兄弟姐妹:
//div[@id="topTenSellers"]//h3[@class="box-title" and .="OODR Items for next 20 Days"]/following::table[@class="table table-hover"]
谢谢安德森。它返回了5个元素。我在最后添加了[1],然后获得了我需要的元素。 – Neela
如果在页面上有多个具有相同类名的'table'节点,您还可以尝试'// div [@ id =“topTenSellers”和.//h3="OODR for next 20 Days“] // table [ @ class =“table table-hover”]' – Andersson
谨慎@Neela,如果您的heiarchy中含有超过1个元素,则在末尾指定[1]仍然可能会导致多个元素。 将你的xpath包裹在()后紧跟着[1]以严格指定单个元素。 '(// div [@ id =“topTenSellers”] // h3 [@ class =“box-title”and。=“OODR Items for next 20 Days”]/following :: table [@ class =“table表悬停“])[1]' – Ben
如果只有一个类名的元素,则任这些将起作用。有一件事需要注意,硒可能会根据版本和浏览器在类名空间中挣扎。如果你发现这种情况,那么使用多个包含来处理空格。
//table[contains(@class,'table table-hover')]
//table[@class = 'table table-hover']
如果您需要的元素作为OODR项目的20天内
//h3[contains(text(),'OODR Items for the next 20 days')]/parent::div/following-sibling::div/table[@class ='table table-hover']
孩子这个路径使用你的锚点,“OODR项目..”,然后去了父母,然后兄弟姐妹,然后到具有指定类名的项目。祝你好运!
请提供xml,而不是图片 – derloopkat
对不起现在加入。 – Neela