我想知道一件事。有一个UI html表与两条记录,并希望点击记录只有准确的值。
例如:SELECT record WHERE tr data-user LIKE "testuser1"
是否有可能用某种简单的方法做到这一点?
<table class="table table-striped">
<thead>
<tr>
<th>
<input type="checkbox" disabled="">
</th>
<th data-sort="status">Status
<i class="fa fa-sort-"></i>
</th>
<th data-sort="name">Name
<i class="fa fa-sort-"></i>
</th>
<th data-sort="position">Position
<i class="fa fa-sort-"></i>
</th>
<th data-sort="userCode">ID
<i class="fa fa-sort-asc"></i>
</th>
<th data-sort="email">e-mail
<i class="fa fa-sort-"></i>
</th>
</tr>
</thead>
<tbody>
<tr data-user="testuser1">
<td>
<input type="checkbox" disabled="">
</td>
<td>
<div class="toggle btn btn-default off btn-sm" data-toggle="toggle" style="width: 76px; height: 30px;">
<input data-user-code="a.anpilov" class="status-toggle" type="checkbox" data-toggle="toggle" data-on="Active" data-off="Inactive" data-size="small">
<div class="toggle-group">
<label class="btn btn-primary btn-sm toggle-on">Active</label>
<label class="btn btn-default btn-sm active toggle-off">Inactive</label><span class="toggle-handle btn btn-default btn-sm"></span></div>
</div>
</td>
<td class="plain">
testuser1
</td>
<td class="plain">
testuser1
</td>
<td class="plain">
testuser1
</td>
<td class="plain">
testuser1
</td>
</tr>
<tr data-user="testuser2">
<td>
<input type="checkbox" disabled="">
</td>
<td>
<div class="toggle btn btn-default off btn-sm" data-toggle="toggle" style="width: 76px; height: 30px;">
<input data-user-code="a.puchaujara" class="status-toggle" type="checkbox" data-toggle="toggle" data-on="Active" data-off="Inactive" data-size="small">
<div class="toggle-group">
<label class="btn btn-primary btn-sm toggle-on">Active</label>
<label class="btn btn-default btn-sm active toggle-off">Inactive</label><span class="toggle-handle btn btn-default btn-sm"></span></div>
</div>
</td>
<td class="plain">
testuser2
</td>
<td class="plain">
testuser2
</td>
<td class="plain">
testuser2
</td>
<td class="plain">
testuser2
</td>
</tr>
</tbody>
</table>
试着这样做:
getDriver().findElement(By.xpath("//div[@id='content'//div[@class='container'//table[@class='table table-striped'//following::tbody//tr[@data-user='testuser1']")).click();
但不工作…(
也许您可以使用xpath表达式使用值属性。例如,选择具有id和value的特定元素并单击它:
driver.findElement(By.xpath("//*[@id='" + yourId + "' and @value='" + yourValue + "']")).click();
或者一个td,里面有一个文本(注意td标签没有值,它里面有一个文本):
driver.findElement(By.xpath("//td[text()='" + yourTdText + "']")).click();
如果你可以建立一个唯一的xpath来识别你正在寻找的东西,这应该是你最好的选择,即使你不知道id或标签类型。
以关闭为准。最终的解决方案:
getDriver().findElement(By.xpath("//tr[@data-user='testuser1']")).click();