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

时间:2014-10-08 20:56:44

标签: c# selenium

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

我尝试了以下但是它不起作用:

var elementEdit = driver.FindElement(By.XPath(" .// tbody [@ClassName =' ng-scope'] / tr [@ ng-binding ='& #34; +用户名+"']"));

以下是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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

例如,如果要选择joetester24 然后xpath如下

// TR [@类= 'NG-范围'] / TD [文本()= 'joetester24']

答案 1 :(得分:0)

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

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的每个元素,直到找到您感兴趣的元素