硒动态增长表

时间:2016-01-04 15:41:17

标签: java selenium selenium-webdriver automated-tests

我有一些麻烦来处理硒的动态增长表。 总而言之,在我的网络应用程序中,我有一个包含30个项目的表,但它只显示了第二个项目,我们必须向下滚动才能显示其余项目。 而且我不知道如何在不向下滚动的情况下获得第26个(例如)项目。

我的HTML:

<table id="tableID" tabindex="0" aria-multiselectable="true" role="grid">
    <thead>
    <tbody id="tableID-tblBody">
        <tr id="item1" role="row" tabindex="-1">
        <tr id="item2" role="row" tabindex="-1">
        [... 17 more]
        <tr id="item20" role="row" tabindex="-1">
    </tbody>
</table>
滚动后

<table id="tableID" tabindex="0" aria-multiselectable="true" role="grid">
    <thead>
    <tbody id="tableID-tblBody">
        <tr id="item1" role="row" tabindex="-1">
        <tr id="item2" role="row" tabindex="-1">
        [... 27 more]
        <tr id="item30" role="row" tabindex="-1">
    </tbody>
</table>

如果有人能帮助我,那就太棒了^^
谢谢!

3 个答案:

答案 0 :(得分:4)

我会 scroll into view表格中的最后一行(如果需要多次,直到获得所需的行数)。为此,我通过scrollIntoView()使用executeScript()

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].scrollIntoView();", element);

其中element

WebElement element = driver.findElements(By.xpath("//table[@id = 'tableID']//tr[last()]")).

答案 1 :(得分:1)

你无法在不向下滚动的情况下获得该项目(向下滚动使用@alecxe解决方案),因为您要选择的元素在搜索时甚至不在html中。 在滚动之后,您应该可以通过id轻松找到它。

答案 2 :(得分:0)

我认为您正在尝试比较值

  

预期值V / s实际值。

要执行此操作,我会要求您获取此表下的所有元素/对象:tableID-tblBody并搜索ID为26并获取值。

这是我用来执行类似任务的代码。

WebDriverWait wait = new WebDriverWait(driver, timeout);
            List<WebElement> totalrow  =  wait.until(ExpectedConditions
                    .presenceOfAllElementsLocatedBy(By.Id("tableID-tblBody"));

        for (int i = 0; i <= totalrow.size(); i++) {

            String xPath ="" //"XPATH OF 26 ITEM";

            try {

                WebElement element = ExpectedBehaviors
                        .expectPresenceofElementBy(driver, By.xpath(xPath),
                                getAlertTimeout());

                if (element.getText().trim().equals("EXPECTED VALUE")) {

                    //"PERFORM YOUR ACTION"                     
                    break;
                }

            } catch (Exception e) {
                //"You can add your exception and error message

            }
        }
相关问题