我正在尝试修复已编写的代码,该代码用于从网页获取网格。当前代码如下:
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的界限”:< / p>
row.addCell(createCell(detailRows.get((grid.getRows()。size()))。cssSelector(),detailRows.get((grid.getRows()。size()))。text() ));
我不确定为什么这行代码在分页之前有效,但在分页之后无效。任何帮助将不胜感激。
编辑:因此事实证明,问题在于网格包含上一页的所有数据,因此当我使用grid.getRows()。size()时,它正在计算所有行,而不仅仅是当前行页。关于如何仅使用当前页面上的行的任何建议?
我可以在拥有所有数据后创建一个新的网格并将其添加到现有网格中吗?
答案 0 :(得分:0)
请注意,List
的大小不等于实际可用的List
索引。对于具有2个元素的List
,您将获得2的大小,但是可以使用list.get(0)
和list.get(1);
接收实际元素。
要解决您的问题,只需从列表大小中减去一个即可:
detailRows.get((grid.getRows().size() - 1)