Selenium WebDriver StaleElementReferenceException。
我们在 Selenium WebDriver 中遇到了 **StaleElementReferenceException**。顾名思义,“stale”是陈旧的意思。可能出现 DOM 中以前存在的元素现在因 DOM 中的修改而不再可用的情况。
在这种情况下,如果我们尝试访问该元素,则会引发 StaleElementReferenceException。由于以下原因,遇到了此类异常 -
元素不再存在于 DOM 中。
元素已被完全删除。
如下所述,我们可以防止 StaleElementReferenceException 的某些方法 -
我们可以重新加载网页并尝试与相同的元素进行交互。
driver.navigate().refresh();
driver.findElement(By.id("txt")).sendKeys("Selenium");我们可以添加 try 捕获块并与相同的元素进行交互。对于循环,将有五次尝试。如果在五次尝试之前识别出该元素,则应退出循环。
for(int k=0; k<=5;k++){
try{
driver.findElement(By.id("txt")).sendKeys("Selenium");
break;
}
catch(Exception exp){
System.out.println(exp.message());
}
}为防止 StaleElementReferenceException,我们可以添加显式等待[同步],借助预期条件 presenceOfElementLocated 来等待元素在 DOM 中呈现。
w.until(ExpectedConditions.presenceOfElementLocated(By.name("presence")));
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP