IndexOutOfBounds在运行的webdriver脚本上随机发生异常

时间:2016-05-25 13:07:02

标签: java selenium selenium-webdriver pageobjects

我正在网页上处理selenium webdriver脚本:http://data.worldbank.org/income-level/HIC

考虑这个实现:

public void retrieveCountryData() throws Exception{
        Thread.sleep(2000);
        WebDriverWait wait = new WebDriverWait(driver, 60); 
        List<WebElement> countryNames = driver.findElements(By.cssSelector("a[href*='/country/']"));
        for(int i =76;i<countryNames.size();i++){
            List<WebElement> countryLinks = driver.findElements(By.cssSelector("a[href*='/country/']"));
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[href*='/country/']")));
            Thread.sleep(3000);
            WebElement elem =countryLinks.get(i);
            JavascriptExecutor js = (JavascriptExecutor)driver;
            js.executeScript("window.scrollTo(" + elem.getLocation().x + "," +(elem.getLocation().y- 100) + ");");

            String countryText = elem.getText();
            System.out.println("The Country is: "+ countryText);
            elem.click();
            wait.until(ExpectedConditions.visibilityOfElementLocated(countryDetailVerify));
            Thread.sleep(2000);
            driver.navigate().back();
}

此脚本有时会正确运行,而在其他时候,它会提供IndexOutOfBounds Excption。我已经运行了这个脚本10次,并且它运行了6次异常,并且运行了整个过程4次。

以下是异常的堆栈跟踪:

java.lang.IndexOutOfBoundsException: Index: 77, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at pageObjects.WorldBankData.retrieveCountryData(WorldBankData.java:172)
    at testCases.SampleTest.getCTex(SampleTest.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1048)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)

at pageObjects.WorldBankData.retrieveCountryData(WorldBankData.java:172)在我的剧本中引用WebElement elem =countryLinks.get(i);

有人可以告诉我为什么我会面对这个问题吗?

1 个答案:

答案 0 :(得分:0)

很难说出你想要完成的事情。我清理了你的方法,并处理索引超出范围的异常。发生异常是因为当countryLinks.get(i)等于大于i总数的值时,将调用countryLinks。因此,如果只有77个国家/地区,因为countryLinks.get(78)调用了i=78,那么就会抛出异常。

@Test
public void retrieveCountryData() throws Exception {
    WebDriver driver = new FirefoxDriver();
    WebDriverWait wait = new WebDriverWait(driver, 15);
    By countrySelector = By.cssSelector("a[href*='/country/']");
    By countryDetailVerify = By.cssSelector("countryDetailVerify?");

    driver.get("http://data.worldbank.org/income-level/HIC");
    List<WebElement> countryNames = driver.findElements(countrySelector);

    for (int i = 76; i < countryNames.size() - 1; i++) {
        List<WebElement> countryLinks = driver.findElements(countrySelector);
        wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(countrySelector));

        WebElement elem = countryLinks.get(i);
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(" + elem.getLocation().x + "," + (elem.getLocation().y - 100) + ");");

        String countryText = elem.getText();
        System.out.println("The Country is: "+ countryText);
        elem.click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(countryDetailVerify));

        driver.navigate().back();
    }
}
相关问题