java.lang.IllegalArgumentException:Sheet index(26)超出范围(0..2)

时间:2017-08-16 09:11:49

标签: java excel selenium-webdriver apache-poi

当我尝试将数据写入Excel工作表时,我超越了Exception。请参阅解决方案以解决我的问题。

请在下面找到我的代码:

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.List;

import java.util.concurrent.TimeUnit;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class CityBusRoutes {

    static String routeName;

    static String routeList;

    public static void main(String[] args) throws IOException {

        System.setProperty("webdriver.chrome.driver",
                "D://Selenium//Selenium Drivers//chromedriver_win32//chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get("http://www.onefivenine.com/busRoute.dont?method=findBusRoute");

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        Select ddl1 = new Select(driver.findElement(By.id("cityId")));

        List<WebElement> elementCount = ddl1.getOptions();

        int citiesCount = elementCount.size();

        for (int i = 1; i <= citiesCount; i++) {

            ddl1.selectByIndex(i);

            Select ddl2 = new Select(driver.findElement(By.id("routeId")));

            List<WebElement> element = ddl2.getOptions();

            int routesCount = element.size();

            for (int j = 1; j <= routesCount; j++) {

                ddl2.selectByIndex(j);

                routeName = driver.findElement(By.xpath("html/body/table/tbody/tr[3]/td[2]/div[2]/table[2]")).getText();

                routeList = driver
                        .findElement(By.xpath("html/body/table/tbody/tr[3]/td[2]/div[2]/table[3]/tbody/tr/td[1]"))
                        .getText();

                FileInputStream input = new FileInputStream("D:\\Citybus-Routes-List.xlsx");

                XSSFWorkbook wBook = new XSSFWorkbook(input);

                XSSFSheet sh = wBook.getSheetAt(citiesCount);

                int rowCount = sh.getLastRowNum() - sh.getFirstRowNum();

                Row newRow = sh.createRow(rowCount + 1);

                newRow.createCell(0).setCellValue(routeName);

                newRow.createCell(1).setCellValue(routeList);

                input.close();

                FileOutputStream output = new FileOutputStream("D:\\Citybus-Routes-List.xlsx");

                wBook.write(output);

                wBook.close();

                output.close();

            }

            driver.quit();
        }

    }
}

以下是我的StackTrace:

Exception in thread "main" java.lang.IllegalArgumentException: Sheet index (26) is out of range (0..3)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.validateSheetIndex(XSSFWorkbook.java:1236)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.getSheetAt(XSSFWorkbook.java:991)
        at CityBusRoutes.main(CityBusRoutes.java:86)

完成一个j循环迭代后,显示以下错误

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
  (Session info: chrome=60.0.3112.90)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 30 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html

1 个答案:

答案 0 :(得分:1)

问题是您正在尝试获取尚未创建的工作表。 尝试替换

XSSFSheet sh = wBook.getSheetAt(citiesCount);

XSSFSheet sh = wBook.createSheet(String.valueOf(i)); //change sheet name if needed