从selenium webDriver中的gui获取动态表数据

时间:2013-08-01 06:04:56

标签: selenium-webdriver

我正在研究一个基于Web的应用程序,我正在使用Selenium进行测试。在一个页面上,内容在表中动态加载。我想得到表格数据,我在这一行中找到了“org.openqa.selenium.NullPointerElementException”。

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>

2 个答案:

答案 0 :(得分: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);;
        }
    }
}

答案 1 :(得分: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;
}