Java 为什么我会得到一个索引越界异常?
我试图修复已经编写的代码,该代码处理从网页获取网格的问题。当前代码如下所示:Java 为什么我会得到一个索引越界异常?,java,grid,indexoutofboundsexception,Java,Grid,Indexoutofboundsexception,我试图修复已经编写的代码,该代码处理从网页获取网格的问题。当前代码如下所示: public static Grid getGrid(String gridXpath) { Grid grid = new Grid(); String html = getWebDriver().findElement(By.xpath(gridXpath)).getAttribute("outerHTML"); Document doc = Jsoup.parse(html);
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);
}
}
publicstaticgridgetgrid(stringgridxpath){
网格=新网格();
字符串html=getWebDriver().findElement(By.xpath(gridXpath)).getAttribute(“outerHTML”);
Document doc=Jsoup.parse(html);
字符串paginationType=getPaginationType(gridXpath);
grid.setPaginationType(paginationType);
//获取标题
元素表=单据选择(“表”).get(0);
元素标题=表格。选择(“th”);
table=doc.select(“table”).get(doc.select(“table”).size()-1);
元素体=表格。选择(“tbody”);
元素行=实体。选择(“tr”);
Elements detailRows=实体。选择(“tr.k-detail-row”);
用于(元素th:标题){
if(th.hasClass(“k-header”)&&!th.hasClass(“k-hierarchy-cell”)){
if(th.html()包含(“类=\“第二行”){
元素子标题=第四个选择第一行(“第二行”);
grid.addSubHeader(subHeader.text());
选择(“第二行”).remove();
grid.addHeader(th.text());
}否则{
grid.addHeader(th.text());
grid.addSubHeader(空);
}
}else如果(!th.hasClass(“k-hierarchy-cell”)){
grid.addHeader(th.text());
grid.addSubHeader(空);
}
}
//添加明细行标题(如果存在)
如果(!detailRows.isEmpty()){
网格。添加标题(“细节”);
网格。添加子标题(“详细信息”);
}
//吵架
for(元素tr:行){
Row Row=grid.new Row();
if(tr.HASSCLASS(“k-master-row”)){
元素单元格=tr.select(“td”);
用于(元素td:单元){
Cell Cell=grid.new Cell();
cell.addCellLocation(td.cssSelector());
if(td.attr(“角色”).equalsIgnoreCase(“网格单元”)){
如果(td.html()包含(“onclick=”)){
元素按钮=td.选择第一个(“按钮”);
cell.addCellControl(button.cssSelector());
td.选择(“按钮”).remove();
}否则{
cell.addCellControl(空);
}
如果(td.html()包含(“span”)){
if(td.html().contains(“class=”第二行“)){
Element subCell=td.selectFirst(“span.second row”);
if(subCell.text().isEmpty()){
cell.setSubText(空);
}否则{
cell.setSubText(subCell.text());
}
td.select(“span.second row”).remove();
if(td.text().isEmpty()){
cell.setText(null);
}否则{
cell.setText(td.text());
}
}否则{
cell.setText(td.select(“span”).text());
td.选择(“span”).remove();
cell.setSubText(td.text());
}
}否则{
cell.setText(td.text());
cell.setSubText(空);
}
row.addCell(cell);
}
}
row.addCell(createCell(detailRows.get((grid.getRows().size())).cssSelector(),detailRows.get((grid.getRows().size())).text());
grid.addRow(row);
}如果(!tr.hasClass(“k-detail-row”)){
元素单元格=tr.select(“td”);
用于(元素td:单元){
Cell Cell=grid.new Cell();
cell.addCellLocation(td.cssSelector());
如果(td.html().包含(“请注意,列表
的大小不等于实际可用的列表
索引。对于具有2个元素的列表
,您将获得2的大小,但实际元素可以通过List.get(0)
和List.get(1);
接收
要解决此问题,只需从列表大小中减去一个:
detailRows.get((grid.getRows().size() - 1)
查看detailRows.get((grid.getRows().size())
,打开调试器,看看为什么detailRows.size()
小于grid.getRows().size()
@Glains detailRows.size()和grid.getRows().size()的值都是10,但是当我输入detailRows.get((grid.getRows().size())时)在调试模式下,我在“表达式”面板中获取该值的索引越界异常。您知道为什么会出现此异常吗?如果两者的大小都为10,这就解释了错误。索引基于0。因此,您可能需要将其更改为:detailRows.get((grid.getRows().size()-1))
我已将此行更改为:row.addCell(createCell(detailRows.get((grid.getRows().size())).cssSelector(),detailRows.get((grid.getRows().size()-1)).text());但是现在我收到错误-'java.lang.IndexOutOfBoundsException:索引10超出长度10'您有多个语句,它们调用了grid.getRows().size()()
。请调整所有代码,而不仅仅是一个。我已经找出了这行代码引发异常的原因,请参阅上面的编辑。如果您对如何修复此问题有任何建议,我们将不胜感激。
detailRows.get((grid.getRows().size() - 1)