[英]Selenium Webdriver - elements only with exact value identification , js/html
我想知道一件事。 有一個包含兩個記錄的UI html表,並且只想單擊具有准確值的記錄。
例如:SELECT記錄WHERE tr數據用戶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();
但是沒有用... :(
也許您可以使用帶有value屬性的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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.