简体   繁体   English

运行网络驱动程序脚本时随机发生IndexOutOfBounds异常

[英]IndexOutOfBounds Exception occurs randomly on running webdriver script

I am working on selenium webdriver script on a webpage: http://data.worldbank.org/income-level/HIC 我正在以下网页上使用Selenium Webdriver脚本: http : //data.worldbank.org/income-level/HIC

Consider this implementation: 考虑以下实现:

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();
}

This script runs correctly sometimes, and at other times, it gives IndexOutOfBounds Excption. 该脚本有时可以正确运行,而在其他时候,它可以提供IndexOutOfBounds Excption。 I have run this script 10 times, and 6 times it gave that exception, and 4 times it runs its whole process. 我已经将该脚本运行了10次,给该异常运行了6次,并运行了整个过程4次。

Following is the stack-trace of the exception: 以下是异常的堆栈跟踪:

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) refers to WebElement elem =countryLinks.get(i); at pageObjects.WorldBankData.retrieveCountryData(WorldBankData.java:172)引用WebElement elem =countryLinks.get(i); in my script. 在我的脚本中。

Can someone tell me why am I facing this issue? 有人可以告诉我为什么我要面对这个问题吗?

It's hard to tell what you're trying to accomplish. 很难说出您要完成什么。 I cleaned up your method and took care of the index out of bounds exception. 我清理了您的方法,并处理了索引超出范围的异常。 The exception was happening because countryLinks.get(i) would get called when i was equal to a value greater than the total number of countryLinks . 发生此异常是因为当i等于大于countryLinks总数的值时,会调用countryLinks.get(i) So if there were only 77 countries, when countryLinks.get(78) was called because i=78 then it would throw the exception. 因此,如果只有77个国家/地区,则因为i=78而调用countryLinks.get(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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM