我试图遍历DOM中的每个表行元素并执行String Assert以查看它是否包含所需的文本。当我调试我的代码并查看我的变量tableRows的Count时,它显示它包含158个项目,而实际页面上几乎没有。我在这里做错了什么?
C#:
[Test]
public void FilterByCompanyTest()
{
const string filter = "Company";
var table = this.Driver.FindElement(By.ClassName("index-table"));
var tableRows = table.FindElements(By.XPath("//tr"));
for (var i = 1; i < tableRows.Count; i++) {
StringAssert.Contains(filter, this.Driver.FindElement(By.XPath("//tr[" + i + "]/td[6]")).Text);
}
}
HTML:
<div class="table-responsive index-table">
<table class="table">
<thead>
<tr>
<th>#</th>
<th class="ticket-title">Title</th>
<th>
<a href="?companyID=-1&technicianID=-1&statusID=0&typeID=&orderBy=priorityAsc">
Priority
<span class="unordered"></span>
</a>
</th>
<th>
<a href="?companyID=-1&technicianID=-1&statusID=0&typeID=&orderBy=statusAsc">
Status
<span class="unordered"></span>
</a>
</th>
<th>
<a href="?companyID=-1&technicianID=-1&statusID=0&typeID=&orderBy=dueDateAsc">
Due Date
<span class="unordered"></span>
</a>
</th>
<th>
<a href="?companyID=-1&technicianID=-1&statusID=0&typeID=&orderBy=companyAsc">
Company
<span class="unordered"></span>
</a>
</th>
<th>
<a href="?companyID=-1&technicianID=-1&statusID=0&typeID=&orderBy=technicianAsc">
Technician
<span class="unordered"></span>
</a>
</th>
<th></th>
</tr>
</thead>
<tbody>
<tr data-id="2">
<td>2</td>
<td class="ticket-title">
Title
</td>
<td>Medium</td>
<td>Active</td>
<td>7/19/2014<br /> <small>(3 changes)</small></td>
<td><a href="#" class="modal-open" data-modal="company-details-modal" data-url="/companies/details/1612">Company</a></td>
<td>
Bill Billington
<span class="glyphicon glyphicon-check"></span>
</td>
<td>
<a href="#" class="modal-open btn btn-sm btn-success" data-modal="edit-modal" data-url="/{controller}/edit/2"><span class="glyphicon glyphicon-edit"></span></a>
<a href="#" class="modal-open btn btn-sm btn-info" data-modal="log-modal" data-url="/{controller}/log/2"><span class="glyphicon glyphicon-list-alt"></span></a>
<a href="#" class="modal-open btn btn-sm btn-warning" data-modal="addtime-modal" data-url="/{controller}/addtime/2"><span class="glyphicon glyphicon-time"></span></a>
</td>
</tr>
<tr data-id="9">
<td>9</td>
<td class="ticket-title">
<span class="work-on-ticket-note-icon-tickets" data-toggle="tooltip" data-placement="right" data-trigger="hover" title="test"></span> Fix this thing!
</td>
<td>Medium</td>
<td>Active</td>
<td>7/23/2014<br /> <small>(1 change)</small></td>
<td><a href="#" class="modal-open" data-modal="company-details-modal" data-url="/companies/details/1604">Company</a></td>
<td>
Bill Billington
<span class="glyphicon glyphicon-check"></span>
<br /><small>Checked In By Bill Billington</small>
</td>
<td>
<a href="#" class="modal-open btn btn-sm btn-success" data-modal="edit-modal" data-url="/{controller}/edit/9"><span class="glyphicon glyphicon-edit"></span></a>
<a href="#" class="modal-open btn btn-sm btn-info" data-modal="log-modal" data-url="/{controller}/log/9"><span class="glyphicon glyphicon-list-alt"></span></a>
<a href="#" class="modal-open btn btn-sm btn-warning" data-modal="addtime-modal" data-url="/{controller}/addtime/9"><span class="glyphicon glyphicon-time"></span></a>
</td>
</tr>
<tr data-id="10">
<td>10</td>
<td class="ticket-title">
<span class="work-on-ticket-note-icon-companies" data-toggle="tooltip" data-placement="right" data-trigger="hover" title="This is a work on ticket note for a company!"></span> Test
</td>
<td>Medium</td>
<td>Active</td>
<td>7/22/2014<br /> <small>(0 changes)</small></td>
<td><a href="#" class="modal-open" data-modal="company-details-modal" data-url="/companies/details/1">Company (Provider)</a></td>
<td>
Bill Billington
</td>
<td>
<a href="#" class="modal-open btn btn-sm btn-success" data-modal="edit-modal" data-url="/{controller}/edit/10"><span class="glyphicon glyphicon-edit"></span></a>
<a href="#" class="modal-open btn btn-sm btn-info" data-modal="log-modal" data-url="/{controller}/log/10"><span class="glyphicon glyphicon-list-alt"></span></a>
<a href="#" class="modal-open btn btn-sm btn-warning" data-modal="addtime-modal" data-url="/{controller}/addtime/10"><span class="glyphicon glyphicon-time"></span></a>
</td>
</tr>
</tbody>
</table>
</div>
答案 0 :(得分:1)
XPath需要更多的帮助来告诉它有关上下文(即在元素内搜索):
var tableRows = table.FindElements(By.XPath(".//tr"));
请注意。。这会将查询过滤到该表中的tr
元素。
另外需要注意的是,这可以在没有XPath的情况下完成:
var tableRows = table.FindElements(By.TagName(“tr”));
我还要提到的是,就性能而言,您可能会发现通过XPath专门定位元素(即不用于循环整个批次)会更快更高效。