java - Selenium 化物 Selenium StaleElementReferenceException : Element not found in the cache

标签 java selenium selenium-webdriver automated-tests selenide

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

我使用了 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"));
        }

最佳答案

您使用网络元素换行:

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 中获取正确的选择器。

关于java - Selenium 化物 Selenium StaleElementReferenceException : Element not found in the cache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36682357/

相关文章:

python selenium点击按钮

java - 如何使用 Avro 和 Flink 解码 Kafka 消息

java - 如何从具有来自另一个类的 string 和 int 值的数组列表中获取特定元素

c# - 迭代 IReadOnlyCollection 中的元素时 FindElement 不迭代

java - 如何使用 Selenium 和 Java 找到并单击选择 boolean 复选框

python - Scraper 无法从网站提取标题

java - keyReleased 函数不恢复 java 中的原始背景颜色

java - 在 CXF 中的传出 SOAP 消息上添加凭据?

python - 在Python、Selenium Webdriver中提取<a>内容

python - 为什么它不发送_keys到评论框?