如何在seleniumjava中通过断言验证动态表



我已经尝试过一个接一个的原始,比如这个

String element= driver.findElement(By.xpath("//*[@id='historyRow']/tbody/tr[1]/td[1]")).getText();
Assert.assertEquals(element, auto);

这是Html代码:

我想验证是否在表中添加第二个值,并验证是否显示了每个值的所有详细信息,并首先添加了以前的值。如何验证所有原始除了一个接一个的,还有其他方法吗

<table id="historyRow">
<thead>
<tr style="cursor: pointer;">
<th class="portlet-section-header descending">
<a href="https://www.uat-office.com/customer/portal/_ns:YWNtLVZpZXdQb2xpdGljYWxEb25hdGlvbi0xX19jbS1WaWV3UG9saXRpY2FsRG9uYXRpb24tMnxjMHxkMXxlZC0yNTA3NTgwLXM9MT0wfGVkLTI1MDc1ODAtcD0xPTF8ZWFjdGlvbj0xPXZpZXdIaXN0b3J5fGVkLTI1MDc1ODAtbz0xPTJ8ZWlkPTE9NGZkMjQyZjY2ZTgyYWY5NTAxNmU4NDE0ZDcyMjE2NmM_/Management/ViewPoliticalDonation.psml" style="text-decoration: none;">Date and Time</a></th>
<th class="portlet-section-header">
<a href="https://www.uat-office.com/customer/portal/_ns:YWNtLVZpZXdQb2xpdGljYWxEb25hdGlvbi0xX19jbS1WaWV3UG9saXRpY2FsRG9uYXRpb24tMnxjMHxkMXxlZC0yNTA3NTgwLXM9MT0xfGVkLTI1MDc1ODAtcD0xPTF8ZWFjdGlvbj0xPXZpZXdIaXN0b3J5fGVkLTI1MDc1ODAtbz0xPTJ8ZWlkPTE9NGZkMjQyZjY2ZTgyYWY5NTAxNmU4NDE0ZDcyMjE2NmM_/Management/ViewPoliticalDonation.psml">User</a></th>
<th class="portlet-section-header">
<a href="https://www.office.com/customer/portal/_ns:YWNtLVZpZXdQb2xpdGljYWxEb25hdGlvbi0xX19jbS1WaWV3UG9saXRpY2FsRG9uYXRpb24tMnxjMHxkMXxlZC0yNTA3NTgwLXM9MT0yfGVkLTI1MDc1ODAtcD0xPTF8ZWFjdGlvbj0xPXZpZXdIaXN0b3J5fGVkLTI1MDc1ODAtbz0xPTJ8ZWlkPTE9NGZkMjQyZjY2ZTgyYWY5NTAxNmU4NDE0ZDcyMjE2NmM_/Management/ViewPoliticalDonation.psml">Action</a></th>
<th class="portlet-section-header">
<a href="https://www.uat-office.com/customer/portal/_ns:YWNtLVZpZXdQb2xpdGljYWxEb25hdGlvbi0xX19jbS1WaWV3UG9saXRpY2FsRG9uYXRpb24tMnxjMHxkMXxlZC0yNTA3NTgwLXM9MT0zfGVkLTI1MDc1ODAtcD0xPTF8ZWFjdGlvbj0xPXZpZXdIaXN0b3J5fGVkLTI1MDc1ODAtbz0xPTJ8ZWlkPTE9NGZkMjQyZjY2ZTgyYWY5NTAxNmU4NDE0ZDcyMjE2NmM_/eManagement/ViewPoliticalDonation.psml">Description / Comment</a></th></tr></thead>
<tbody>
<tr class="portlet-section-body" style="cursor: pointer;">
<td>
Nov 19, 2019 09:34:10 AM EST
</td>
<td>
<!-- This hack is needed just for requests which had this audit comment inserted by their rule execution -->
<!-- New requests won't have this comment anymore -->

abc,auto
</td>
<td>
Edit
</td>
<td>


<table style="width: auto;">

<tbody><tr style="cursor: pointer;"><td align="left" style="border-spacing:1px; padding-top:0px; padding-right:3px; padding-bottom:0px; padding-left:0px;">
<table style="border-spacing:1px; padding-top:0px; padding-right:3px; padding-bottom:0px; padding-left:0px; width: auto;">
<tbody><tr style="cursor: pointer;">
<td align="left" style="border-spacing:1px; padding-top:0px; padding-right:3px; padding-bottom:0px; padding-left:0px; vertical-align:text-top; text-align:left;">
<b>Comment</b>
</td>
<td style="border-spacing:1px; padding-top:0px; padding-right:3px; padding-bottom:0px; padding-left:0px;">
Add Comment.  comment  
</td>
</tr>
</tbody></table>

尝试以下代码:请检查大括号的闭合情况。

// Grab the table 
WebElement table = driver.findElement(By.id("View"));
//get the size of the list of the rows
List<WebElement> totalRows = table.findElements(By.tagName("tr"));
int rows_count = totalRows.size();
//Loop will execute till the last row of table.
for (int row=0; row<rows_count; row++){
//To locate columns(cells) of that specific row.
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
//To calculate no of columns(cells) In that specific row.
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row "+row+" are "+columns_count);
//Loop will execute till the last cell of that specific row.
for (int column=0; column<columns_count; column++){
//To retrieve text from that specific cell.
String celtext = Columns_row.get(column).getText();
System.out.println("Cell Value Of row number "+row+" and column number "+column+" Is "+celtext); 
}
});

最新更新