【发布时间】:2016-05-31 15:37:33
【问题描述】:
我在这里遇到了某种情况。
我有一个函数 editNewUser()。
public void editNewUser() throws Exception
{
driver.findElement(adminModuleDD).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(searchRes));
List<WebElement> elements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(uNames));
for(WebElement el : elements)
{
if(el.getText().equals(UserName))
{
el.click();
wait.until(ExpectedConditions.visibilityOfElementLocated(editUserHeading));
driver.findElement(editUser).click();
WebElement status_Dropdown = driver.findElement(By.xpath("//select[@id='systemUser_status']"));
Select status = new Select(status_Dropdown);
status.selectByVisibleText("Enabled");
}
}
}
UserName 是一个公共字符串变量,在创建用户期间在另一个函数中获取值。
在这个脚本中,我导航到一个包含用户列表的页面,我将所有用户名存储在列表“元素”中,然后遍历列表中文本与用户名匹配的每个元素。
现在,在我的 test_run 脚本中,我在 editNewUser() 之后调用了一些其他方法。
事情是这样的,这个方法在 if 块中执行以下命令。
if(el.getText().equals(UserName))
{
el.click();
wait.until(ExpectedConditions.visibilityOfElementLocated(editUserHeading));
driver.findElement(editUser).click();
WebElement status_Dropdown = driver.findElement(By.xpath("//select[@id='systemUser_status']"));
Select status = new Select(status_Dropdown);
status.selectByVisibleText("Enabled");
}
但是一旦调用 next 方法,它就会停止执行并抛出org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
堆栈跟踪指的是if(el.getText().equals(UserName))这一行。
你能告诉我为什么我会收到这个异常,尽管执行了 if 块中的命令。
【问题讨论】:
-
当 el.getText().equals(UserName) 条件为真时,您正在执行导航操作。因此,列表中的其余元素更改其位置或可能页面已更改,但在selenium 缓存作为异常发生..如果你正在实现这一点,如果条件为真,你应该打破循环..
标签: java selenium selenium-webdriver exception-handling staledataexception