根据表标题为表数据创建XPATH

根据表标题为表数据创建XPATH,xpath,selenium-webdriver,Xpath,Selenium Webdriver,我有一张如下所示的桌子: 在某些情况下,可能缺少某些列,因此我们为获取表数据而创建的XPath将获取不正确的数据 例如:如果我需要获取总收入,Xpth将是 //*[@id='sessionForm:dataTable_data']/tr/td[7]/div 但是,假设“Total Revenue”字段前面的一列丢失,那么XPath将更改为/*[@id='sessionForm:dataTable_data']/tr/td[6]/div,但在我的脚本中,它将为不正确的列提供值 表格后面的HTML

我有一张如下所示的桌子:

在某些情况下,可能缺少某些列,因此我们为获取表数据而创建的XPath将获取不正确的数据

例如:如果我需要获取总收入,Xpth将是

//*[@id='sessionForm:dataTable_data']/tr/td[7]/div

但是,假设“Total Revenue”字段前面的一列丢失,那么XPath将更改为
/*[@id='sessionForm:dataTable_data']/tr/td[6]/div
,但在我的脚本中,它将为不正确的列提供值

表格后面的HTML如下所示:

<div class="ui-datatable-scrollable-body" tabindex="-1" style="margin-right: 0px; width: 680px;">
 <table role="grid">
   <thead class="ui-datatable-scrollable-theadclone" style="height: 0px;">
     <tr role="row">
      <th id="sessionForm:dataTable:userid_clone" class="ui-state-default ui-sortable-column" style="width:58px" aria-label="User Id: activate to sort column ascending" role="columnheader" tabindex="0" aria-sort="other">
        <span class="ui-column-title">User Id</span>
      </th>
      <th id="sessionForm:dataTable:Enterprisename_clone" class="ui-state-default ui-sortable-column" style="width:70px" aria-label="Enterprise: activate to sort column ascending" role="columnheader" tabindex="0" aria-sort="other">
        <span class="ui-column-title">Enterprise</span>
      </th>
      <th id="sessionForm:dataTable:startTime_clone" class="ui-state-default ui-sortable-column" style="width:56px" aria-label="Start Time: activate to sort column ascending" role="columnheader" tabindex="0" aria-sort="other">
        <span class="ui-column-title">Start Time</span>
      </th>
      <th id="sessionForm:dataTable:endTime_clone" class="ui-state-default ui-sortable-column" style="width:60px" aria-label="End Time: activate to sort column ascending" role="columnheader" tabindex="0" aria-sort="other">
        <span class="ui-column-title">End Time</span>
      </th>
      <th id="sessionForm:dataTable:dynamicColumns:1" class="ui-state-default ui-sortable-column" style="width:80px" aria-label="Sale Amt: activate to sort column ascending" role="columnheader" aria-sort="other">
        <span class="ui-column-title">Sale Amt</span>
        <span class="ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s"/>
      </th>
      <th id="sessionForm:dataTable:dynamicColumns:2" class="ui-state-default ui-sortable-column" style="width:80px" aria-label="Session ID: activate to sort column ascending" role="columnheader" aria-sort="other">
        <span class="ui-column-title">Session ID</span>
        <span class="ui-sortable-column-icon ui-icon ui-icon-carat-2-n-s"/>
      </th>
    </tr>
 </thead>
<tbody id="sessionForm:dataTable_data" class="ui-datatable-data ui-widget-content">
<tr class="ui-widget-content ui-datatable-even" role="row" data-ri="0">
<td role="gridcell">
<span title="test_user@mailinator.com1044">test_user@test.com</span>
</td>
<td role="gridcell">
<span title="test_enterprise">test_enterise</span>
</td>
<td role="gridcell">
<div class="numeric">01/29/2018 16:00:00 </div>
</td>
<td role="gridcell">
<div class="numeric">01/29/2018 21:00:00 </div>
</td>
<td role="gridcell">
<span title="0.0">0.0</span>
</td>
<td role="gridcell">
<span title="41">41</span>
</td>
</tr>
<tr class="ui-widget-content ui-datatable-odd" role="row" data-ri="1">
<td role="gridcell">
<span title="abc_user@mailinator.com2754">abc_user@test.com</span>
</td>
<td role="gridcell">
<span title="abc_enterprise">abc_enterprise</span>
</td>
<td role="gridcell">
<div class="numeric">01/23/2018 14:30:00 </div>
</td>
<td role="gridcell">
<div class="numeric">01/23/2018 15:45:00 </div>
</td>
<td role="gridcell">
<span title="0.0">0.0</span>
</td>
<td role="gridcell">
<span title="40">40</span>
</td>
</tr>

用户Id
企业
开始时间
结束时间
销售金额
会话ID
试验_user@test.com
测试企业
01/29/2018 16:00:00 
01/29/2018 21:00:00 
0
41
abc_user@test.com
abc_企业
01/23/2018 14:30:00 
01/23/2018 15:45:00 
0
40
这是部分HTML代码,仅供参考


我需要根据表格标题为表格列创建XPath,即首先读取表格标题,并根据标题上的位置获取该列的所有值。

您可以使用类似以下内容:

//*[@id='sessionForm:dataTable_data']/tr/td[position()=count(///span[@ui-column-title'和text()='Total Revenue']/../previous sibling::*)+1]/div

在这个xpath中,它将获得总收入的列号,然后使用它来获得该列值。因此,假设一列在它之前消失,那么该值将自动减少1

希望这有帮助。

Java示例:

// List of title columns
List<WebElement> columns = driver.findElements(By.xpath(".//span[@class='ui-column-title']"));
int i = 1;
boolean stop = False;
WebElement elem = null;
while(i<columns.size() && !stop){
    elem = columns.get(i-1);
    // if the elem has the title stop the loop
    if (elem.getText().equalsIgnoreCase("Total Revenue")){
        stop = True;
    }
}
// The i variable has the right position to take the values for the column
List<WebElement> Revenue_rows = driver.findElements(By.xpath('.//td[(position() > (i-1)) AND (position()<= i)]'))
//标题列列表
List columns=driver.findElements(By.xpath(“.//span[@class='ui-column-title']);
int i=1;
布尔停止=假;
WebElement elem=null;

虽然(I请将HTML代码设置为可读这不起作用,但如果我单独执行查询以获取元素的位置计数(//span[@ui-column-title'和text()='End Time']/../previous sibling::*),它始终会返回第一列的值+1,那么它工作得很好。但是当执行完整的XPath时,它就不工作了,只返回第一列valuesThanks,这个想法对我很有效,现在我可以根据表头获取表的数据。