为什么我的索引超出界限范围?

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

我正在尝试修复已编写的代码,该代码处理从网页获取网格。目前代码如下所示:

 public static Grid getGrid(String gridXpath) {

    Grid grid = new Grid();
    String html = getWebDriver().findElement(By.xpath(gridXpath)).getAttribute("outerHTML");
    Document doc = Jsoup.parse(html);

    String paginationType = getPaginationType(gridXpath);
    grid.setPaginationType(paginationType);

    // get headers
    Element table = doc.select("table").get(0);
    Elements headers = table.select("th");
    table = doc.select("table").get(doc.select("table").size() - 1);
    Elements bodies = table.select("tbody");
    Elements rows = bodies.select("tr");
    Elements detailRows = bodies.select("tr.k-detail-row");

    for (Element th : headers) {
        if (th.hasClass("k-header") && !th.hasClass("k-hierarchy-cell")) {
            if (th.html().contains("class=\"second-row\"")) {
                Element subHeader = th.selectFirst("span.second-row");
                grid.addSubHeader(subHeader.text());
                th.select("span.second-row").remove();
                grid.addHeader(th.text());
            } else {
                grid.addHeader(th.text());
                grid.addSubHeader(null);
            }
        } else if (!th.hasClass("k-hierarchy-cell")) {
            grid.addHeader(th.text());
            grid.addSubHeader(null);
        }
    }
    // add detail row header if it exists
    if (!detailRows.isEmpty()) {
        grid.addHeader("detail");
        grid.addSubHeader("detail");
    }

    // get rows
    for (Element tr : rows) {
        Row row = grid.new Row();
        if (tr.hasClass("k-master-row")) {
            Elements cells = tr.select("td");
            for (Element td : cells) {
                Cell cell = grid.new Cell();
                cell.addCellLocation(td.cssSelector());
                if (td.attr("role").equalsIgnoreCase("gridcell")) {
                    if (td.html().contains("onclick=")) {
                        Element button = td.selectFirst("button");
                        cell.addCellControl(button.cssSelector());
                        td.select("button").remove();
                    } else {
                        cell.addCellControl(null);
                    }
                    if (td.html().contains("span")) {
                        if (td.html().contains("class=\"second-row\"")) {
                            Element subCell = td.selectFirst("span.second-row");
                            if (subCell.text().isEmpty()) {
                                cell.setSubText(null);
                            } else {
                                cell.setSubText(subCell.text());
                            }
                            td.select("span.second-row").remove();
                            if (td.text().isEmpty()) {
                                cell.setText(null);
                            } else {
                                cell.setText(td.text());
                            }
                        } else {
                            cell.setText(td.select("span").text());
                            td.select("span").remove();
                            cell.setSubText(td.text());
                        }
                    } else {
                        cell.setText(td.text());
                        cell.setSubText(null);
                    }
                    row.addCell(cell);
                }
            }
            row.addCell(createCell(detailRows.get((grid.getRows().size())).cssSelector(), detailRows.get((grid.getRows().size())).text()));
            grid.addRow(row);
        } else if (!tr.hasClass("k-detail-row")) {
            Elements cells = tr.select("td");
            for (Element td : cells) {
                Cell cell = grid.new Cell();
                cell.addCellLocation(td.cssSelector());
                if (td.html().contains("<input")) {
                    Element input = td.selectFirst("input");
                    cell.setText(null);
                    cell.setSubText(null);
                    cell.addCellControl(input.cssSelector());

                } else {
                    cell.setText(td.text());
                    cell.setSubText(null);
                    cell.addCellControl(null);
                }
                row.addCell(cell);

                if (td.hasAttr("colspan")) {
                    for (int i = 1; i < Integer.valueOf(td.attr("colspan")); i++) {
                        row.addCell(createCell(null, null));
                    }
                }
            }
            grid.addRow(row);
        }
    }

如果网格上只有一页数据,则此代码可以正常工作,但是当有多个数据时,它会丢失。为了解决这个问题,我试图添加一些分页代码。到目前为止,我有以下内容:

if (paginationType != null && getCurrentPage(gridXpath, grid.getPaginationType()) != getLastPageNumberShownInPagination(gridXpath, grid.getPaginationType())) {
        do {
            gotoNextPage(gridXpath, grid.getPaginationType());

            html = getWebDriver().findElement(By.xpath(gridXpath)).getAttribute("outerHTML");
            doc = Jsoup.parse(html);


            table = doc.select("table").get(0);
            table = doc.select("table").get(doc.select("table").size() - 1);
            bodies = table.select("tbody");
            rows = bodies.select("tr");
            detailRows = bodies.select("tr.k-detail-row");

            for (Element tr : rows) {
                Row row = grid.new Row();
                if (tr.hasClass("k-master-row")) {
                    Elements cells = tr.select("td");
                    for (Element td : cells) {
                        Cell cell = grid.new Cell();
                        cell.addCellLocation(td.cssSelector());
                        if (td.attr("role").equalsIgnoreCase("gridcell")) {
                            if (td.html().contains("onclick=")) {
                                Element button = td.selectFirst("button");
                                cell.addCellControl(button.cssSelector());
                                td.select("button").remove();
                            } else {
                                cell.addCellControl(null);
                            }
                            if (td.html().contains("span")) {
                                if (td.html().contains("class=\"second-row\"")) {
                                    Element subCell = td.selectFirst("span.second-row");
                                    if (subCell.text().isEmpty()) {
                                        cell.setSubText(null);
                                    } else {
                                        cell.setSubText(subCell.text());
                                    }
                                    td.select("span.second-row").remove();
                                    if (td.text().isEmpty()) {
                                        cell.setText(null);
                                    } else {
                                        cell.setText(td.text());
                                    }
                                } else {
                                    cell.setText(td.select("span").text());
                                    td.select("span").remove();
                                    cell.setSubText(td.text());
                                }
                            } else {
                                cell.setText(td.text());
                                cell.setSubText(null);
                            }
                            row.addCell(cell);
                        }
                    }
                    row.addCell(createCell(detailRows.get((grid.getRows().size())).cssSelector(), detailRows.get((grid.getRows().size())).text()));
                    grid.addRow(row);
                } else if (!tr.hasClass("k-detail-row")) {
                    Elements cells = tr.select("td");
                    for (Element td : cells) {
                        Cell cell = grid.new Cell();
                        cell.addCellLocation(td.cssSelector());
                        if (td.html().contains("<input")) {
                            Element input = td.selectFirst("input");
                            cell.setText(null);
                            cell.setSubText(null);
                            cell.addCellControl(input.cssSelector());

                        } else {
                            cell.setText(td.text());
                            cell.setSubText(null);
                            cell.addCellControl(null);
                        }
                        row.addCell(cell);

                        if (td.hasAttr("colspan")) {
                            for (int i = 1; i < Integer.valueOf(td.attr("colspan")); i++) {
                                row.addCell(createCell(null, null));
                            }
                        }
                    }
                    grid.addRow(row);
                }
            }

这些代码大部分只是前一代码的复制/粘贴,但是当我运行它时,我收到错误'java.lang.IndexOutOfBoundsException:索引10超出以下行的长度3':

row.addCell(createCell(detailRows.get((grid.getRows()。size()))。cssSelector(),detailRows.get((grid.getRows()。size()))。text()));

我不确定为什么这行代码在分页之前有效,但之后无效。任何帮助将不胜感激。

编辑:事实证明问题是网格包含来自上一页的所有数据,因此当我使用grid.getRows()。size()时,它计算所有行而不是当前页面中的行。关于如何只使用当前页面上的行的任何建议?

我可以创建一个新网格,并在拥有所有数据后将其添加到现有网格中吗?

java grid indexoutofboundsexception
1个回答
0
投票

请注意,List的大小不等于实际可用的List指数。对于具有2个元素的List,您将获得2的大小,但实际元素可以使用list.get(0)list.get(1);接收。

要解决您的问题,只需从列表大小中减去一个:

detailRows.get((grid.getRows().size() - 1) 
© www.soinside.com 2019 - 2024. All rights reserved.