Selenium - 无法确定错误java.lang.IndexOutOfBoundsException:索引:92,大小:92

时间:2016-06-28 00:06:29

标签: java selenium

我理解这个问题已被问过几次,但我已经尝试了大部分解决方案并最终发布...如果错过任何请直接感谢。

我正在尝试浏览页面并抓取标签中的所有链接('a')并点击每个显示的链接,出于某些原因我得到了

java.lang.IndexOutOfBoundsException:Index:92,Size:92

 public static void clickOnEachLinkOnAPage(String tagName, String homePageTitle) {

    int numberOfElementsFound = getNumberOfElementsFound(By.tagName(tagName));
    System.out.println(numberOfElementsFound);
    for (int pos = 0; pos < numberOfElementsFound; pos++) {

        if (getElementWithIndex(By.tagName(tagName), pos).isDisplayed()) {
            String linkText = getElementWithIndex(By.tagName(tagName), pos).getText().trim();
            String url = getElementWithIndex(By.tagName(tagName), pos).getAttribute("href");
        if (linkText.length()!=0) {
                getElementWithIndex(By.tagName(tagName), pos).click();
                String newWindow = driver.getWindowHandle();
                Generic.handleMultipleWindows(newWindow);
                String pageTitle =  driver.getTitle();
                linkText = StringUtils.abbreviate(linkText, 10);
                System.out.println(pos +","+linkText+","+url+","+pageTitle);
               // System.out.println(linkText+","+url+","+pageTitle);
                closeAllOtherWindows(newWindow );
            System.out.println("number of elements"+numberOfElementsFound);
                if(!pageTitle.equals(homePageTitle)) {
                    driver.navigate().back();
                }
            }
        }
    }
}



public static int getNumberOfElementsFound(By by) {
    return driver.findElements(by).size();
}


public static WebElement getElementWithIndex(By by, int pos) {
    return driver.findElements(by).get(pos);
}

   public static boolean closeAllOtherWindows(String openWindowHandle) {
    Set<String> allWindowHandles = driver.getWindowHandles();
    for (String currentWindowHandle : allWindowHandles) {
        if (!currentWindowHandle.equals(openWindowHandle)) {
            driver.switchTo().window(currentWindowHandle);
            driver.close();
        }
    }

    driver.switchTo().window(openWindowHandle);
    if (driver.getWindowHandles().size() == 1)
        return true;
    else
        return false;
}

    public static void handleMultipleWindows(String windowTitle) {
    Set<String> windows = driver.getWindowHandles();

    for (String window : windows) {
        driver.switchTo().window(window);
        if (driver.getTitle().contains(windowTitle)) {
            return;
        }
    }
}

我收到错误

java.lang.IndexOutOfBoundsException:Index:92,Size:92

1 个答案:

答案 0 :(得分:0)

我修复了我的代码如下并且工作正常...谢谢大家。我改变了一点让它工作。

public static void clickOnEachLinkOnAPage(String tagName, String homePageTitle) {

    int numberOfElementsFound = getNumberOfElementsFound(By.tagName(tagName));
    // System.out.println(numberOfElementsFound);
    String currentWindow = driver.getWindowHandle();
    for (int pos = 0; pos < numberOfElementsFound; pos++) {
        String linkText = getElementWithIndex(By.tagName(tagName), pos).getText().trim();
        if (linkText.length() != 0) {
            String url = getElementWithIndex(By.tagName(tagName), pos).getAttribute("href");
            getElementWithIndex(By.tagName(tagName), pos).click();
            String newWindow = driver.getWindowHandle();
            Generic.handleMultipleWindows(newWindow);
            String pageTitle = driver.getTitle();
            linkText = StringUtils.abbreviate(linkText, 10);
            //   System.out.println(pos +","+linkText+","+url+","+pageTitle);
            System.out.println(linkText + "," + url + "," + pageTitle);
            closeAllOtherWindows(newWindow);
            Generic.handleMultipleWindows(currentWindow);
            String pageTitleCurrent = driver.getTitle();
            if (!pageTitleCurrent.equals(homePageTitle)) {
                driver.navigate().back();
            }
        }
    }
}