>我有一个表格,如下所示:包含标题和数据的表
在某些情况下,可能会缺少某些列,因此我们为获取表数据而创建的 XPath 将获取不正确的数据。
例如:如果我需要获取总收入,则 Xpth 将是
.//*[@id='sessionForm:dataTable_data']/tr/td[7]/div
但是假设如果缺少"总收入"字段之前的一列,则 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>
这是部分 HTML 代码,仅供参考。
我需要根据表标题为表列创建 XPath,即首先我读取表标题并根据标题上的位置,我需要获取该列的所有值。
你可以使用这样的东西:
.//*[@id='sessionForm:dataTable_data']/tr/td[position()=count(//span[@class='ui-column-title' and text()='Total Revenue']/../preceding-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)]'))