【问题标题】:xpath with following-sibling具有以下兄弟姐妹的 xpath
【发布时间】:2017-10-20 12:28:04
【问题描述】:

我想访问类名为“table table-hover”的表,该表应位于 class=='box-title' 下,其中包含文本“OODR Items for next 20 Days”。任何人都可以帮我获得xpath吗?我尝试了跟随兄弟,但没有运气。提前致谢。

<?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>

【问题讨论】:

  • 请提供xml,而不是图片
  • 抱歉现在添加了。

标签: selenium xpath automation


【解决方案1】:

您可以尝试使用following 而不是following-sibling,正如提到的h3table 节点不是兄弟节点:

//div[@id="topTenSellers"]//h3[@class="box-title" and .="OODR Items for next 20 Days"]/following::table[@class="table table-hover"]

【讨论】:

  • 谢谢安德森。它返回了 5 个元素。我最后添加了 [1] 然后我得到了我需要的元素。
  • 如果页面上有多个table节点类名相同,也可以试试//div[@id="topTenSellers" and .//h3="OODR Items for next 20 Days"]//table[@class="table table-hover"]
  • 警告@Neela,如果您有超过 1 个这些元素的层次结构,则在末尾指定 [1] 仍可能导致多个元素。将您的 xpath 包装在 () 后跟 [1] 以严格指定单个元素。 (//div[@id="topTenSellers"]//h3[@class="box-title" and .="OODR Items for next 20 Days"]/following::table[@class="table table-hover"])[1]
【解决方案2】:

如果该类名只有一个元素,那么其中任何一个都可以使用。不过需要注意的一件事是,根据版本和浏览器的不同,selenium 可能会在类名中出现空格。如果您发现是这种情况,则使用多个包含来处理空格。

//table[contains(@class,'table table-hover')] 
//table[@class = 'table table-hover'] 

如果您在接下来的 20 天内需要该元素作为该 OODR 项的子项

//h3[contains(text(),'OODR Items for the next 20 days')]/parent::div/following-sibling::div/table[@class ='table table-hover']

此路径使用您的锚点“OODR Items..”,然后转到父级,然后是兄弟级,然后是具有指定类名的项目。祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多