我有一张表格如下所示: table with header and data
在某些情况下,某些列可能会丢失,因此我们为获取表数据而创建的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>
这是部分HTML代码,仅供参考。
我需要根据表标题为表列创建XPath,首先我读取表头并根据标题上的位置获取该列的所有值。
答案 0 :(得分:1)
您可以使用以下内容:
.//*[@id='sessionForm:dataTable_data']/tr/td[position()=count(//span[@class='ui-column-title' and text()='Total Revenue']/../preceding-sibling::*)+1]/div
在此xpath中,它将获取Total Revenue的列号,然后使用它来获取该列值。因此,假设一列在它之前消失,那么该值将自动减少1.
希望这有帮助。
答案 1 :(得分:0)
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)]'))