2015-07-28 58 views
2
<div id="OmContentSection" class="modified_content ui-content"   role="main" data-role="content" style="padding: 0px;">  
    <div> 
    <div id="positionsContainer" class="WL_watchlist_listitem"> 
    <table id="tblMainData" class="WL_watchlist_table" border="0" width="100%" cellspacing="0" cellpadding="0"> 
     <tbody id="positionsList"> 
     <tr id="A"> 
      <td id="A_daysToExpiry" class="fieldRendererText "/> 
      <td id="A_targetProfit" class="fieldRendererText "/> 
      <td id="A_stopLossPrice" class="fieldRendererText "/> 
      <td id="A_note" class="fieldRendererText "/> 
      <td id="A markChange" class="fieldRendererText ">-2.13</td> 
      <td id="A_markPercChange" class="fieldRendererText ">-5.18%  </td></tr> 
     <tr id="AA"> 
     <tr id="AL"> 
     <tr id="AR"> 
     <tr id="AT"> 
     <tr id="AM"> 
     <tr id="AT"> 
     <tr id="BAC"> 
     <tr id="M"> 
     </tbody> 
     </table> 
</div> 

`無法獲得行數和使用webdriver的2.0訪問表的表數據(JAVA)

下面是我的代碼來處理表中的數據:

WebElement table = driver.findElement(OH_positions); 


    WebElement tablebody = driver.findElement(By.id("positionsList")); 

     // Now get all the TR elements from the table 
     List<WebElement> allRows = tablebody.findElements(By.tagName("tr")); 
     System.out.println("row size :"+allRows.size()); 

     // And iterate over them, getting the cells 
     for (WebElement row : allRows) { 
      List<WebElement> cells = row.findElements(By.tagName("td")); 
      int columncount = cells.size(); 
      System.out.println("Column size :"+columncount); 
      System.out.println("data:"+cells.get(row); 


     } 

在我的代碼我已經嘗試了幾種方法來識別表格和行 但是我在每個實例中將行大小設置爲0。每次我都得到與'0'相同的響應,我不知道如何解決這個問題。

幫助表示讚賞。

+0

你缺少收盤''和幾個關''秒 - 複製和粘貼錯誤? –

+0

是的Basti ..你是對的有一個錯字錯誤。 –

+0

根據上述評論所做的更改。 –

回答

0

我猜行數不對。您正嘗試使用cells.get()而不是提供索引,而不是傳遞WebElement。試試如下

// And iterate over them, getting the cells 
for (WebElement row : allRows) { 
    List cells = row.findElements(By.tagName("td")); 
    int columncount = cells.size(); 
    System.out.println("Column size :" + columncount); 
    int cols = row.findElements(By.tagName("td")).size(); 
    System.out.println("Column size :"+cols);  
} 
+0

謝謝您輸入Saifur。 –