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

fabbymee發表於2015-09-17

html部分程式碼如下:

<tbody>
         <tr class="odd">
            <td>1609</td>
            <td>-YOUKU-begin</td>
            <td>firstAgent</td>
            <td> </td>
            <td> </td>
            <td>2015-09-16 17:02:55</td>
            <td><span class="stop" ref="1609" id="change_status"></span>stop </td>
            <td class="mtbuttons"><a href="javascript:;">permission</a> | <a href="/code/index.php?r=update&id=1609">edit</a> </td>
         </tr>
         <tr class="even selected">
            <td>1594</td>
            <td>-YOUKU</td>
            <td>firstAgent</td>
            <td> </td>
            <td> </td>
            <td>2015-09-16 17:02:55</td>
            <td><span class="stop" ref="1594" id="change_status"></span>stop </td>
            <td class="mtbuttons"><a href="javascript:;">permission</a> | <a href="/code/index.php?r=update&id=1594">edit</a> </td>
         </tr>
         <tr class="odd">
            <td>1593</td>
            <td>-YOUKU-first</td>
            <td>firstAgent</td>
            <td> </td>
            <td> </td>
            <td>2015-09-16 17:02:55</td>
            <td><span class="stop" ref="1593" id="change_status"></span>stop </td>
            <td class="mtbuttons"><a href="javascript:;">permission</a> | <a href="/code/index.php?r=update&id=1593">edit</a> </td>
         </tr>
      </tbody>
找到列中有1594這個值的行,然後點選edit連結

List<WebElement> trlist=driver.findElements(By.xpath("//div[@id='company-grid']/table/tbody/tr"));
		    for(int r=0;r<trlist.size();r++){
		    	String trText=trlist.get(r).getText();
		    	if(trText.contains("1594")){
		    		trlist.get(r).findElement(By.xpath("(.//a[contains(text(),'edit')])[2]")).click();
		    		
		    		break;
		    	}
		    	
		    }





相關文章