如何在Selenium中使用Xpath选择记录

本文关键字:Xpath 选择 记录 Selenium | 更新日期: 2023-09-27 18:25:56

我有一个特定的记录,我正试图从数据网格中选择。可以用XPath帮助获取该记录吗。

我尝试了以下方法,但不起作用:

var elementEdit=驱动程序。FindElement(通过.XPath(".//tbody[@ClassName='ng-scope']/tr[@ng binding='"+用户名+"']");

这是HTML代码:

<tbody>
  <!-- ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">pppp</td>
    <td class="ng-binding">ppp</td>
    <td class="ng-binding">joetester24</td>
    <td class="ng-binding">+16195555555</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">gg</td>
    <td class="ng-binding">ggg</td>
    <td class="ng-binding">joetester99 ggff66</td>
    <td class="ng-binding">+18587660891</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Evelina</td>
    <td class="ng-binding">Papantoniou</td>
    <td class="ng-binding">joetester996</td>
    <td class="ng-binding">+12015555555</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Parvati</td>
    <td class="ng-binding">Shallow</td>
    <td class="ng-binding">joetester990</td>
    <td class="ng-binding">+12015555555</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Doutzen</td>
    <td class="ng-binding">Kroes</td>
    <td class="ng-binding">joetester998</td>
    <td class="ng-binding">+12015555555</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Evelina</td>
    <td class="ng-binding">Papantoniou</td>
    <td class="ng-binding">joetester996</td>
    <td class="ng-binding">+12015555555</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Tereza</td>
    <td class="ng-binding">Benesova</td>
    <td class="ng-binding">joetester995</td>
    <td class="ng-binding">+17036928692</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Ashley</td>
    <td class="ng-binding">Degenford</td>
    <td class="ng-binding">joetester992</td>
    <td class="ng-binding">+17036976251</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Joe</td>
    <td class="ng-binding">Tester114</td>
    <td class="ng-binding">joetester114</td>
    <td class="ng-binding">+16195551212</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Jodi Ann</td>
    <td class="ng-binding">Paterson</td>
    <td class="ng-binding">joetester994</td>
    <td class="ng-binding">+17036951997</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Leilani</td>
    <td class="ng-binding">Dowding</td>
    <td class="ng-binding">joetester993</td>
    <td class="ng-binding">+17036149998</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Naureen</td>
    <td class="ng-binding">Zaim</td>
    <td class="ng-binding">joetester991</td>
    <td class="ng-binding">+17036957508</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Joe</td>
    <td class="ng-binding">Tester445</td>
    <td class="ng-binding">joetester445</td>
    <td class="ng-binding">+16195551212</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Joe</td>
    <td class="ng-binding">Tester229</td>
    <td class="ng-binding">joetester229</td>
    <td class="ng-binding">+16195551212</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Juliet</td>
    <td class="ng-binding">Cariaga</td>
    <td class="ng-binding">joetester997</td>
    <td class="ng-binding">+17032715280</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Joe</td>
    <td class="ng-binding">Tester825</td>
    <td class="ng-binding">joetester825</td>
    <td class="ng-binding">+16195551212</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">shdh</td>
    <td class="ng-binding">dff</td>
    <td class="ng-binding">joetesteroqur</td>
    <td class="ng-binding">+16195553164</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">hdjd</td>
    <td class="ng-binding">xddd</td>
    <td class="ng-binding">joetester0183</td>
    <td class="ng-binding">+16195553164</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">djd</td>
    <td class="ng-binding">dff</td>
    <td class="ng-binding">joetesterm</td>
    <td class="ng-binding">+16195553164</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">gf</td>
    <td class="ng-binding">ff</td>
    <td class="ng-binding">joetesterqww</td>
    <td class="ng-binding">+16195553164</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">hhh</td>
    <td class="ng-binding">aaaa</td>
    <td class="ng-binding">joetester6p</td>
    <td class="ng-binding">+16195553164</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">Parvati</td>
    <td class="ng-binding">Shallow</td>
    <td class="ng-binding">joetester990</td>
    <td class="ng-binding">+12015555555</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">hshs</td>
    <td class="ng-binding">fff</td>
    <td class="ng-binding">joetesterhdhd</td>
    <td class="ng-binding">+16195554785</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">test</td>
    <td class="ng-binding">test</td>
    <td class="ng-binding">joetester234</td>
    <td class="ng-binding">+16195551207</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
  <tr class="ng-scope" ng-repeat="user in model">
    <td class="ng-binding">test</td>
    <td class="ng-binding">test</td>
    <td class="ng-binding">joetester789</td>
    <td class="ng-binding">+16195551207</td>
    <td class="ng-binding">
      <img class="map-icon" src="../../media/images/google-maps-icon.png" ng-click="viewMap(user.lastKnownPosition.lat, user.lastKnownPosition.lon)">0, 0
    </td>
    <td class="text-center" ng-click="open(user)"><span class="fa fa-edit"></span>
    </td>
  </tr>
  <!-- end ngRepeat: user in model -->
</tbody>

如何在Selenium中使用Xpath选择记录

例如,如果您想选择joetester24则xpath如下

//tr[@class='ng-scope']/td[text()='joetester24']

您可以尝试使用循环遍历每个元素。

var trr = driver.findElements(By.XPath("*//tr[@class='ng-scope']"));
forEach(Iwebelement tr in trr){
   var tdd = tr.driver.findElements(By.XPath("*//td[@class='ng-binding']"));
      forEach(Iwebelement td in tdd){
         if(td.text == "YOUR_MATCHING_TEXT"){// do operations}
         // else continue;
      }
}

这将查看HTML DOM的每个元素,直到找到感兴趣的元素为止