2013-08-01 19 views
1

我正在使用Selenium測試基於Web的應用程序。在一個頁面上,內容被動態加載到表格中。我想獲取表格數據,我在這一行中創建了一個「org.openqa.selenium.NullPointerElementException」。從gui中獲取動態表格數據webDriver

WebElement table = log.driver.findElement(By.xpath(tableXpath)); 

我試了下面的完整代碼。

public int selectfromtable(String tableXpath, String CompareValue, int columnnumber) throws Exception { 
    WebElement table = log.driver.findElement(By.xpath(tableXpath)); 
    List<WebElement> rows = table.findElements(By.tagName("tr")); 
    int flag = 0; 
    for (WebElement row : rows) { 
     List<WebElement> cells = row.findElements(By.tagName("td")); 
     if (!cells.isEmpty() && cells.get(columnnumber).getText().equals(CompareValue)) { 
      flag = 1; 
      Thread.sleep(1000); 
      break; 
     } else { 
      Thread.sleep(2000); 
      flag = 0; 
     } 
    } 
    return flag; 
} 

我打電話上述方法類似

String tableXpath = ".//*[@id='event_list']/form/div[1]/table/tbody/tr/td/div/table"; 
selectfromtable(tableXpath, eventType, 3); 

我的html頁面是一樣

<table width="100%"> 
    <tbody style="overflow: auto; background-color: #FFFFFF"> 
     <tr class="trOdd"> 
      <td width="2%" align="center"> 
      <td width="20%" align="center"> Account </td> 
      <td width="20%" align="center"> Enter Collection </td> 
      <td width="20%" align="center"> 
      <td width="20%" align="center"> 10 </td> 
      <td width="20%" align="center"> 1 </td> 
     </tr> 
    </tbody> 
    <tbody style="overflow: auto; background-color: #FFFFFF"> 
     <tr class="trEven"> 
      <td width="2%" align="center"> 
      <td width="20%" align="center"> Account </td> 
      <td width="20%" align="center"> Resolved From Collection </td> 
      <td width="20%" align="center"> 
      <td width="20%" align="center"> 10 </td> 
      <td width="20%" align="center"> 1 </td> 
     </tr> 
    </tbody> 
</table> 
+1

你可能需要提供更多的html,你向我們展示了'table'元素,但是你沒有包含這些元素。可能在你到達'table'之前,xpath'不正確。 –

+0

@Mark如果XPath格式不正確,它會拋出一個不同的錯誤,而不是一個NullPointerException,如果它沒有找到任何東西,它會拋出一個NoSuchElementException異常。 –

回答

0

我有類似的問題。我找到的解決方案是一起使用方法和xpath。

public void theSearch(String value) throws Exception { 
    String xpathExpression = "//*[starts-with(@id,'searchResultsTable:')]"; 
    List<WebElement> elementTable= state.getDriver().findElements(By.xpath(xpathExpression)); 

    for (WebElement listofElement : elementTable) { 
     String theElement= listofElement.getText(); 

     if (theElement.contains(value)) { 
      Assert.assertEquals(value, theElement); 
      // System.out.println("The Expected Value " + value + " Equals the actual " + theElement);; 
     } 
    } 
} 
0

我對類似案例的解決方案 - 我想從表中得到一個行號,給出的列中包含特定的文本。

// Method searches in given column of a table and return number of row where 
// exactly first value is spotted (title row counts as 0 row and is 
// skipped). If no value is found then 0 is returned. Given column number 
// starts with 1. 

public Integer getTableRowNumberWithValue(String tableId, String value, 
     Integer columnNumber) { 

    WebElement table = getDriver().findElement(By.id(tableId)); 
    List<WebElement> rows = table.findElements(By.tagName("tr")); 

    int j = 0; 
    int i = 0; 
    for (WebElement row : rows) { 
     // Skip title row which counts as 0 row. 
     if (i > 0) { 
      if (row.findElements(By.tagName("td")).get(columnNumber - 1) 
        .getText().equals(value)) { 
       j = i; 
       break; 
      } 
     } 
     i++; 
    } 
    return j; 
}