有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有WebElements的java Selenium Webdriver列表

您好,我必须从页面中保存相同的<a>,并已获得为class=my_img我将此元素保存在列表中,在我尝试进入列表的第一个元素后,返回后获得第二个元素,但selenium给我此错误

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up

这是我的密码

    List <WebElement> Element = drivers.findElements(By.cssSelector(".my_img"));
    System.out.println("Megethos"+Element.size());
    System.out.println("Pame stous epomenous \n");
    for (i = 1; i < Element.size(); i++) {
        drivers.manage().timeouts().implicitlyWait(35, TimeUnit.SECONDS);
        System.out.println(i+" "+Element.size());
        System.out.println(i+" "+Element.get(i));
        action.click(Element.get(i)).perform();
        Thread.sleep(2000);
        System.out.println("go back");
        drivers.navigate().back();
        Thread.sleep(6000);
        drivers.navigate().refresh();
        Thread.sleep(6000);

    }

共 (2) 个答案

  1. # 1 楼答案

    如果主要目的是单击链接并返回上一页,则最好获取页面中所有“a”元素的“href”属性,并导航到每个元素。当您导航回原始DOM更改时,您所遵循的方式将始终导致StaleElementReferenceExeception

    下面是我建议的方法:

    List<WebElement> linkElements = driver.findElements(By.xpath("//a[@class='my_img']"));
    System.out.println("The number of links under URL is: "+linkElements.size());
    
    //Getting all the 'href' attributes from the 'a' tag and putting into the String array linkhrefs
    String[] linkhrefs = new String[linkElements.size()];
    int j = 0;
    for (WebElement e : linkElements) {
        linkhrefs[j] = e.getAttribute("href");
        j++;
    }
    
    // test each link
    int k=0;
    for (String t : linkhrefs) {
        try{
            if (t != null && !t.isEmpty()) {
                System.out.println("Navigating to link number "+(++k)+": '"+t+"'");
                driver.navigate().to(t);
                String title;
                title = driver.getTitle();
                System.out.println("title is: "+title);
    
                //Some known errors, if and when, found in the navigated to page.
                if((title.contains("You are not authorized to view this page"))||(title.contains("Page not found"))
                                ||(title.contains("503 Service Unavailable"))
                                ||(title.contains("Problem loading page")))
                {
                System.err.println(t + " the link is not working because title is: "+title);
                } else {
                    System.out.println("\"" + t + "\"" + " is working.");
                }
            }else{
                System.err.println("Link's href is null.");
            }
        }catch(Throwable e){
    
            System.err.println("Error came while navigating to link: "+t+". Error message: "+e.getMessage());
        }
    
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    }
    
  2. # 2 楼答案

    你的行动。click()和/或navigate()调用将导致页面重新加载,从而导致列表中的WebElement不再有效。将findElements()调用放入循环中:

    List <WebElement> Element = drivers.findElements(By.cssSelector(".my_img"));
    for (i = 1; i < Element.size(); i++) {
        Element = drivers.findElements(By.cssSelector(".my_img"));
        drivers.manage().timeouts().implicitlyWait(35, TimeUnit.SECONDS);
        System.out.println(i+" "+Element.size());
        System.out.println(i+" "+Element.get(i));
        action.click(Element.get(i)).perform();
        Thread.sleep(2000);
        System.out.println("go back");
        drivers.navigate().back();
        Thread.sleep(6000);
        drivers.navigate().refresh();
        Thread.sleep(6000);
    
    }