我得到了StaleElementReferenceException.在以下情况下,我如何解决这个问题。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我得到了StaleElementReferenceException.在以下情况下,我如何解决这个问题。相关的知识,希望对你有一定的参考价值。

'''System.setProperty("webdriver.chrome.driver", "C:/Users/NaqDaq/Downloads/chromedriver_win32/chromedriver.exe"); WebDriver driver = new ChromeDriver();

    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();

    driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.get("https://www.amazon.com");

    List<WebElement> linksList = driver.findElements(By.tagName("a"));
    linksList.addAll(driver.findElements(By.tagName("img")));

    System.out.println("The full size of Links and Images are: " + linksList.size());

    List<WebElement> activeLinks = new ArrayList<WebElement>();

    for(int i=0; i<linksList.size(); i++) {
        //System.out.println(linksList.get(i).getAttribute("href"));
        if(linksList.get(i).getAttribute("href") != null) {
            activeLinks.add(linksList.get(i));
        }
    }

    System.out.println("The active links and images are--->" + activeLinks.size());

'''

答案

Vishal,

试试下面的代码,对我有用。

@Test(enabled = true)
public static void amazonActiveLinks() {

    // Browser Initializing
    System.setProperty("webdriver.gecko.driver", "C:\\Software\\geckodriver-v0.19.0-win64\\geckodriver.exe");

    driver = new FirefoxDriver();

    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();

    driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    driver.get("https://www.amazon.com");

    List<WebElement> linksList = driver.findElements(By.tagName("img"));
    linksList.addAll(driver.findElements(By.tagName("a")));
    System.out.println("The full size of Links and Images are: " + linksList.size());

    String url;
    List<WebElement> activeLinks = new ArrayList<WebElement>();

    for (WebElement link : linksList) {
        url = link.getAttribute("href");
        if (url != null || url != "") {
            activeLinks.add(link);
        }
    }
    System.out.println("The active links and images are--->" + activeLinks.size());

}

以上是关于我得到了StaleElementReferenceException.在以下情况下,我如何解决这个问题。的主要内容,如果未能解决你的问题,请参考以下文章

Python Selenium StaleElementReferenceException:消息:过时的元素引用:元素未附加到页面文档

关于报错stale element reference: element is not attach

stale element reference: element is not attached to the page document 异常

关于报错stale element reference: element is not attached to the page document处理

我拿到webShells 了,但怎么能得到权限呢?有人教教我吗?我出500分。。。

我怎样才能得到 Gson 的解析?我尝试这样做,但我得到了一个 Expected BEGIN_ARRAY 但是是 BEGIN_OBJECT