selenium用java找到表格某一行某一列中含有特定文字的某个元素

时间:2021-04-24 15:03:34

html部分代码如下:

 

[html] view plain copy
 
  1. <tbody>  
  2.          <tr class="odd">  
  3.             <td>1609</td>  
  4.             <td>-YOUKU-begin</td>  
  5.             <td>firstAgent</td>  
  6.             <td</td>  
  7.             <td</td>  
  8.             <td>2015-09-16 17:02:55</td>  
  9.             <td><span class="stop" ref="1609" id="change_status"></span>stop </td>  
  10.             <td class="mtbuttons"><href="javascript:;">permission</a> | <href="/code/index.php?r=update&id=1609">edit</a</td>  
  11.          </tr>  
  12.          <tr class="even selected">  
  13.             <td>1594</td>  
  14.             <td>-YOUKU</td>  
  15.             <td>firstAgent</td>  
  16.             <td</td>  
  17.             <td</td>  
  18.             <td>2015-09-16 17:02:55</td>  
  19.             <td><span class="stop" ref="1594" id="change_status"></span>stop </td>  
  20.             <td class="mtbuttons"><href="javascript:;">permission</a> | <href="/code/index.php?r=update&id=1594">edit</a</td>  
  21.          </tr>  
  22.          <tr class="odd">  
  23.             <td>1593</td>  
  24.             <td>-YOUKU-first</td>  
  25.             <td>firstAgent</td>  
  26.             <td</td>  
  27.             <td</td>  
  28.             <td>2015-09-16 17:02:55</td>  
  29.             <td><span class="stop" ref="1593" id="change_status"></span>stop </td>  
  30.             <td class="mtbuttons"><href="javascript:;">permission</a> | <href="/code/index.php?r=update&id=1593">edit</a</td>  
  31.          </tr>  
  32.       </tbody>  

找到列中有1594这个值的行,然后点击edit链接

 

 

[java] view plain copy
 
  1. List<WebElement> trlist=driver.findElements(By.xpath("//div[@id='company-grid']/table/tbody/tr"));  
  2.             for(int r=0;r<trlist.size();r++){  
  3.                 String trText=trlist.get(r).getText();  
  4.                 if(trText.contains("1594")){  
  5.                     trlist.get(r).findElement(By.xpath("(.//a[contains(text(),'edit')])[2]")).click();  
  6.                       
  7.                     break;  
  8.                 }  
  9.                   
  10.             }