简体   繁体   中英

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.

Something like for instance: SELECT record WHERE tr data-user 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. For example, to select certain element with id and value and click on it:

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

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.

Subject to close. Final solution:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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