我必须打开页面中的所有链接,然后检查它是否有文本(“无信息”)。
我使用了 Selenide v3.5 和 Selenium v2.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/