- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含一堆表格的页面。我在外循环中遍历表,然后在内循环中遍历表中的每一行。一切正常。但是有些页面有下一步按钮。当我在完成页面后添加代码以单击它时,我开始在循环遍历表格的行时收到 StaleElementReferenceException。
代码如下:
WebDriverWait wait1 = new WebDriverWait(driver, 10000);
WebElement maxPage = null;
WebElement auctionsWaitingDiv = driver.findElement(By.cssSelector("div[class='Head_W']"));
if (auctionsWaitingDiv.isDisplayed() == false) return properties;
try {
maxPage = wait1.until(ExpectedConditions.visibilityOfElementLocated(By.id("maxWA")));
} catch (TimeoutException ex) {
return properties;
}
Integer maxPageNo = 1;
if (!maxPage.getText().isEmpty())
maxPageNo = Integer.parseInt(maxPage.getText());
for (int i = 1; i <= maxPageNo; i++) {
driver.findElement(By.cssSelector("div[id='Area_W']")); //only look at Auctions Waiting section
WebDriverWait wait2 = new WebDriverWait(driver, 10000);
List<WebElement> tables = null;
try {
tables = wait2.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("table[class='ad_tab']")));
} catch (TimeoutException ex) {
System.out.println("table not found in allotted time");
return properties;
} catch (StaleElementReferenceException ex) {
System.out.println("returning due to StaleElementReferenceException");
return properties;
}
for (WebElement table: tables) {
List<String> propAttributes = new ArrayList<>();
// StaleElementReferenceException: The element reference of
// <table class="ad_tab"> is stale; either the element is no
// longer attached to the DOM, it is not in the current
// frame context, or the document has been refreshed
List<WebElement> rows = table.findElements(By.cssSelector("tr"));
String parcelLink = "";
for (WebElement row : rows) {
WebElement key = row.findElement(By.cssSelector("th"));
WebElement val = row.findElement(By.cssSelector("td"));
String keyVal = key.getText() + val.getText();
propAttributes.add(keyVal);
if (key.getText().equals("Parcel ID:")) {
WebElement a = val.findElement(By.cssSelector("a"));
parcelLink = a.getAttribute("href");
}
}
}
driver.findElement(By.xpath(".//*[@class='PageRight']")).click(); //click the "Next" button
}
我不明白为什么陈旧的元素会发生?页面在循环期间没有改变,我一直等到所有元素都被获取。如何避免 StaleElementReferenceException?
编辑:最后的堆栈跟踪显示它发生在这一行:
List<WebElement> rows = table.findElements(By.cssSelector("tr"));
上面的错误信息显示:
SEVERE: null
org.openqa.selenium.StaleElementReferenceException: The element reference of
<table class="ad_tab">
is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed
最佳答案
StaleElementReferenceException每当你想访问一个不再可用的元素引用时抛出。当元素不再附加到 DOM 或页面已更新时,就会发生这种情况。
解决方法就是每当发生这种情况时再次搜索该元素。您可以调整所有测试或页面对象。或者您编写自己的 RobustWebDriver 和 RobustWebElement,它们会在抛出 SERE 时刷新元素。
RobustWebDriver:
public class RobustWebDriver implements WebDriver {
private WebDriver originalWebDriver;
public RobustWebDriver(WebDriver webDriver) {
this.originalWebDriver = webDriver;
}
@Override
public void get(String url) {
this.originalWebDriver.get(url);
}
@Override
public String getCurrentUrl() {
return this.originalWebDriver.getCurrentUrl();
}
@Override
public String getTitle() {
return this.originalWebDriver.getTitle();
}
@Override
public List<WebElement> findElements(By by) {
List<WebElement> elements = new ArrayList<>();
for (WebElement element : this.originalWebDriver.findElements(by)) {
elements.add(new RobustWebElement(element, by, this));
}
return elements;
}
@Override
public WebElement findElement(By by) {
return new RobustWebElement(this.originalWebDriver.findElement(by), by, this);
}
@Override
public String getPageSource() {
return this.originalWebDriver.getPageSource();
}
@Override
public void close() {
this.originalWebDriver.close();
}
@Override
public void quit() {
this.originalWebDriver.quit();
}
@Override
public Set<String> getWindowHandles() {
return this.originalWebDriver.getWindowHandles();
}
@Override
public String getWindowHandle() {
return this.originalWebDriver.getWindowHandle();
}
@Override
public TargetLocator switchTo() {
return this.originalWebDriver.switchTo();
}
@Override
public Navigation navigate() {
return this.originalWebDriver.navigate();
}
@Override
public Options manage() {
return this.originalWebDriver.manage();
}
}
RobustWebElement:
public class RobustWebElement implements WebElement {
private WebElement originalElement;
private RobustWebDriver driver;
private By by;
private static final int MAX_RETRIES = 10;
public RobustWebElement(WebElement element, By by, RobustWebDriver driver) {
this.originalElement = element;
this.by = by;
this.driver = driver;
}
@Override
public void click() {
int retries = 0;
while (retries < MAX_RETRIES) {
try {
this.originalElement.click();
return;
} catch (StaleElementReferenceException ex) {
refreshElement();
}
retries++;
}
throw new StaleElementReferenceException(
String.format("Element is still stale after %s retries.", MAX_RETRIES));
}
@Override
public void sendKeys(CharSequence... keysToSend) {
int retries = 0;
while (retries < MAX_RETRIES) {
try {
this.originalElement.sendKeys(keysToSend);
return;
} catch (StaleElementReferenceException ex) {
refreshElement();
}
retries++;
}
throw new StaleElementReferenceException(
String.format("Element is still stale after %s retries.", MAX_RETRIES));
}
// TODO add other unimplemented methods with similar logic.
private void refreshElement() {
this.originalElement = driver.findElement(by);
}
然后您只需要将您的 WebDriver 包装到 RobustWebDriver 中就可以了:
WebDriver driver = new RobustWebDriver(new ChromeDriver());
编辑:
当然你需要自己照顾上下滚动。
关于java - Selenium:循环遍历一组元素时如何避免 StaleElementReferenceException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57512299/
我刚刚开始使用带有 webdriver 的 geb 进行自动化测试。 As I understand it ,当我在页面上定义内容时,每次调用内容定义时都应该查找页面元素。 //In the cont
我有一个非常简单的脚本,可以找到页面中的所有 HREF 并迭代并记录它: Photo search scroll Go To https://xxx ${elements}=
我正在为鼠标悬停工作,我想通过使用 for 循环单击每个链接来测试所有链接的工作状况。在我的程序中,迭代进行一次,而对于下一次迭代,它不起作用并显示“StaleElementReferenceExce
在我的网页上,我有一个部分链接列表,每个部分都有详细信息的链接。我尝试转到每个部分,然后验证所有链接都没有损坏。 List sections = driver.findElements(By.xpat
我有一个场景,我试图循环遍历条形图上的多个元素,直到找到“rect”标签名称。当我单击“矩形”标签名称时,将从图表中选择单个栏,并且我将重定向到另一个页面。请参阅下面我正在使用的条形图的图像: /im
我已经研究这个错误一段时间了,并尝试了很多方法,但似乎没有任何效果...... while(!driver.findElements(By.className("next")).isE
您好,我是 Selenium 的新手 我正在使用 Java 库,已经尝试了 Chrome 和 Firefox 驱动程序。 我正在运行一个循环。有趣的是,循环有时工作 3、2 次,它并不总是在同一次迭代
我正在测试新建的框架,在 Chrome 浏览器中工作时经常遇到 org.openqa.selenium.StaleElementReferenceException。框架设计会不会有问题?当我在其他浏
显示堆栈溢出错误 public static void main(String[] args) throws Exception { System.setProperty("webdriver
我在这样的测试中有wait: WebDriverWait wait = new WebDriverWait(dr, TimeSpan.FromSeconds(30)); ... wait.Until(
好吧,伙计们...我知道这个问题之前似乎已经得到解答,但我尝试了几种解决方案,但没有一个奏效。如果我再看到一次 StaleElementReferenceException,我就会丢掉它。我妻子的男朋
导航到不同页面并返回后,我收到 StaleElementReferenceException。我尝试过显式等待,再次重写定位器,但似乎不起作用。任何帮助将不胜感激。 Select select
我尝试用selenium 制作一个网络爬虫。我的程序引发 StaleElementReferenceException。我认为这是因为我递归地抓取页面,并且当页面没有更多链接时,该函数导航到下一页,而
我需要获取快速变化元素的值(在本例中是一个以毫秒为单位倒计时的计时器),但是当我尝试使用时 String timeLeftString = driver.findElement(By.xpath(ti
我有一个包含一堆表格的页面。我在外循环中遍历表,然后在内循环中遍历表中的每一行。一切正常。但是有些页面有下一步按钮。当我在完成页面后添加代码以单击它时,我开始在循环遍历表格的行时收到 StaleEle
在我的 Selenium 测试代码中有几行 点击一个复选框 从选择框中选择一个项目 点击按钮提交表单 这是代码 WebElement selectAllElement = driver.findEle
我在 Ubunto 上运行 Web E2E 测试(使用 cucumber、junit、selenium webDriver)。 使用remoteWebDriver时,我的测试偶尔失败(使用local-
我一直在使用 protractor/jasmine2 解决陈旧元素引用异常的问题。 我的规范: var LoginPage = require('../pages/login_page.js'); v
这个问题已经有答案了: Stale element exception python (2 个回答) Selenium clicks one time, but next click returns
运行测试时出现此错误:org.openqa.selenium.StaleElementReferenceException: 元素不再附加到 DOM 关于如何解决上述异常的任何想法?这发生在我的网格中
我是一名优秀的程序员,十分优秀!