【问题标题】:Selenide-selenium StaleElementReferenceException: Element not found in the cacheSelenide-selenium StaleElementReferenceException:在缓存中找不到元素
【发布时间】:2016-08-09 12:07:09
【问题描述】:

我必须打开页面中的所有链接,然后检查它是否有文字(“无信息”)。

我使用了 Selenide v3.5 和 Selenium v​​2.53 库。但是,我有这个异常“StaleElementReferenceException”。

我的代码如下:

    open(url);
        WebDriverRunner.clearBrowserCache();

        WebDriverRunner.getWebDriver().manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        List<WebElement> linksize = WebDriverRunner.getWebDriver().findElements(By.tagName("a"));

        linksCount = linksize.size();
        links = new String[linksCount];
        Common.consoleOutput.printMessage(String.format("Total no of links Available: %d", linksCount));

        for (int i = 0; i < linksCount; i++)
        {
            links[i] = linksize.get(i).getAttribute("href");

        }
        // navigate to each Link on the webpage
        for (int i = 0; i < linksCount; i++) {
            WebDriverRunner.getWebDriver().navigate().to(url_arabic);
            WebElement error = $(Selectors.byText("No info"));
            $(error).shouldNotBe(visible).shouldNotBe(text("No info"));
        }

【问题讨论】:

    标签: java selenium selenium-webdriver automated-tests selenide


    【解决方案1】:

    您使用 web 元素包装:

    WebElement error = $(Selectors.byText("No info"));
    $(error).shouldNotBe(visible).shouldNotBe(text("No info"));
    

    尝试直接使用选择器包装:

    $(Selectors.byText("No info")).shouldNotBe(visible).shouldNotBe(text("No info"));
    

    它应该有帮助。我不推荐使用$(WebElement),因为,selenide 执行重新搜索元素。如果第一次没有找到元素,并且通过 selenium 设计很难从 WebElement 中获取正确的选择器。

    【讨论】:

    • 谢谢,但仍然不适用于 firefox 45.0.1 我从代码中删除了这部分,但仍然给出异常。我删除了 for (int i = 0; i
    • 错误跟踪将异常的原因指向这一行 links[i] = linkssize.get(i).getAttribute("href");
    【解决方案2】:

    在我使用以下代码获取链接元素后,它起作用了:

    List<String> links = new ArrayList<>();
            for (SelenideElement link : $$("a"))
                links.add(link.attr("href"));
    

    而不是

            List<WebElement> linksize = WebDriverRunner.getWebDriver().findElements(By.tagName("a"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多