无法获取行计数并使用webdriver 2.0(Java)访问表中的表数据

时间:2015-07-28 10:54:33

标签: selenium-webdriver

 <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>

`

以下是处理表格数据的代码:

   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”相同的响应。我不知道如何解决这个问题。

对此表示感谢。

1 个答案:

答案 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);    
}