简体   繁体   English

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

[英]Selenium Webdriver - elements only with exact value identification , js/html

I am wondering about one thing. 我想知道一件事。 Have a UI html table with two records and would like to click on a record only with exact value. 有一个包含两个记录的UI html表,并且只想单击具有准确值的记录。

Something like for instance: SELECT record WHERE tr data-user LIKE "testuser1" 例如:SELECT记录WHERE tr数据用户LIKE“ testuser1”

Is it possible to do that in some simple way ? 是否可以通过一些简单的方式做到这一点?

<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>

Tried to do it like that: 试图这样做:

getDriver().findElement(By.xpath("//div[@id='content'//div[@class='container'//table[@class='table table-striped'//following::tbody//tr[@data-user='testuser1']")).click();

but did not work ... :( 但是没有用... :(

Maybe you can use xpath expression using value attribute. 也许您可以使用带有value属性的xpath表达式。 For example, to select certain element with id and value and click on it: 例如,要选择具有id和value的某些元素,然后单击它:

driver.findElement(By.xpath("//*[@id='" + yourId + "' and @value='" + yourValue + "']")).click();

Or a td whith a text inside (be aware that td tag has no value, it has a text inside): 或td里面有一个文本(请注意,td标签没有任何价值,它里面有一个文本):

driver.findElement(By.xpath("//td[text()='" + yourTdText + "']")).click();

If you can build an unique xpath to identify what you are looking for this should be your best option even if you don't know the id or the tag type. 如果您可以构建一个唯一的xpath来标识您要查找的内容,即使您不知道ID或标签类型,这也是最好的选择。

Subject to close. 以关闭为准。 Final solution: 最终解决方案:

getDriver().findElement(By.xpath("//tr[@data-user='testuser1']")).click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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