繁体   English   中英

Selenium Webdriver-仅具有精确值标识的元素,js / html

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM