获得从GUI动态表数据中硒的webdriver

问题描述 投票:1回答:2

我的工作,我使用Selenium测试一个基于Web的应用。在一个页面上的内容是动态加载表。我想要得到的数据表格,我在歌厅此行是“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>
selenium-webdriver
2个回答
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;
}

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);;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.