Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium-无法确定错误java.lang.IndexOutOfBoundsException:索引:92,大小:92_Java_Selenium - Fatal编程技术网

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

Selenium-无法确定错误java.lang.IndexOutOfBoundsException:索引:92,大小:92,java,selenium,Java,Selenium,我知道这个问题已经被问了好几次了,但我已经尝试了大多数解决方案,最后发布了。。。如果错过了,请直接告诉我谢谢 我试图进入一个页面,抓取标签('a')中的所有链接,并单击每个显示的链接,出于某种原因,我得到了一些信息 java.lang.IndexOutOfBoundsException:索引:92,大小:92 public static void clickOnEachLinkOnAPage(String tagName, String homePageTitle) { int nu

我知道这个问题已经被问了好几次了,但我已经尝试了大多数解决方案,最后发布了。。。如果错过了,请直接告诉我谢谢

我试图进入一个页面,抓取标签('a')中的所有链接,并单击每个显示的链接,出于某种原因,我得到了一些信息

java.lang.IndexOutOfBoundsException:索引:92,大小: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;
        }
    }
}
public static void clickOnEachLinkOnAPage(字符串标记名,字符串主页标题){
int numberOfElementsFound=getNumberOfElementsFound(按.tagName(标记名));
System.out.println(numberOfElementsFound);
对于(int pos=0;pos
我犯了一个错误


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;
        }
    }
}
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();
            }
        }
    }
}
public static void clickOnEachLinkOnAPage(字符串标记名,字符串主页标题){
int numberOfElementsFound=getNumberOfElementsFound(按.tagName(标记名));
//System.out.println(numberOfElementsFound);
字符串currentWindow=driver.getWindowHandle();
对于(int pos=0;pos
您正在使用大小为92的数组访问索引92。索引92不存在,因为数组是以零为基的。哪一行会导致此错误?我想根本原因是加载页面后删除了一些链接。当我在调试模式下运行时——我的测试在if(getElementWithIndex(By.tagName(tagName),pos).isDisplayed()处失败{@Andrew,谢谢,但我的问题是——在一个页面中,我找到了98个元素,但错误显示为java.lang.IndexOutOfBoundsException:Index:92,Size:92——这对我来说毫无意义me@user790049在方法
getElementWithIndex
中,您能否在返回之前打印此
driver.findElements(by).size();