Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

web table handling selenium webdriver java

     <table class="footable table table-hover table-striped table-bordered" 
     cellspacing="0" cellpadding="6" border="0">
     <thead>
     <tr class="CartBlueHeader">
     <th align="10%">PNR No</th>
     <th width="23%" align="center">Origin</th>
     <th width="22%" align="center">Destination</th>
     <th width="10%">Departure</th>
     <th width="10%">Return</th>
     <th width="10%">Amount</th>
     <th width="15%"/>
     </tr>
   </thead>
    <tbody>
      <tr class="BGLightblue font11">
       <td align="left">   Q2S2SO </td>
       <td align="left">   Dubai Intl Airport </td>
       <td align="left">   Hindustan Airport </td>
       <td align="center"> 30 Sep 17 </td>
       <td align="center">-</td>
       <td align="left"> 608.00 SAR   </td>
       <td align="left">
    </tr>
    </tbody>
    </table>

Want to retrieve the values elements based on its header value in the table. How do i proceed with this any idea.

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Follow the pattern only

// Grab the table 
WebElement table = driver.findElement(By.id("divListView")); 

// Now get all the TR elements from the table 
List<WebElement> allRows = table.findElements(By.tagName("tr")); 

// And iterate over them, getting the cells 
for (WebElement row : allRows) { 
    List<WebElement> cells = row.findElements(By.tagName("td")); 

    // Print the contents of each cell
    for (WebElement cell : cells) { 
        System.out.println(cell.getText());
    }
}

Hope it will help you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...