【发布时间】:2020-10-08 12:50:17
【问题描述】:
线程“main”中的异常 org.openqa.selenium.StaleElementReferenceException: 过时的元素引用:元素未附加到页面文档 (会话信息:chrome=83.0.4103.106) 有关此错误的文档,请访问:https://www.seleniumhq.org/exceptions/stale_element_reference.html 构建信息:版本:'3.141.59',修订:'e82be7d358',时间:'2018-11-14T08:25:53' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能 {acceptInsecureCerts: false, browserName: chrome, browserVersion: 83.0.4103.106, chrome: {chromedriverVersion: 83.0.4103.39 (ccbf011cb2d2b..., userDataDir: C:\Users\MOHAMM~1\AppData\L...}, goog :chromeOptions: {debuggerAddress: localhost:53846}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, 超时: {implicit: 0,pageLoad:300000,脚本:30000},unhandledPromptBehavior:关闭并通知,webauthn:virtualAuthenticators:true} 会话 ID:f9fd73fa86d306099bad2835337c25c0 *** 元素信息:{Using=xpath, value=//h3[@class='LC20lb DKV0Md']}
结果显示在第一页,但是当我点击第二页结果时,我得到了 StaleElementReferenceException
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class NavigateToGoogle {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//driver.findElement(By.xpath("//input[@class='gLFyf gsfi'])")).sendKeys("zahid");
WebElement element=driver.findElement(By.name("q"));
element.sendKeys("mohammed zahid");
//element.click();
Thread.sleep(3000);
//driver.findElement(By.xpath("(//input[@type='submit'])[3]")).click();
List<WebElement> button=driver.findElements(By.name("btnK"));
System.out.println(button.size());
WebElement submit=driver.findElement(By.name("btnK"));
submit.submit();
WebElement matchingRes=driver.findElement(By.xpath("//h3[@class='LC20lb DKV0Md']"));
List<WebElement> listRes=matchingRes.findElements(By.xpath("//h3[@class='LC20lb DKV0Md']"));
System.out.println(listRes.size());
for(WebElement results:listRes)
{
String value = results.getText();
System.out.println(value);
}
driver.findElement(By.xpath("//a[@aria-label='Page 2']")).click();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h3[@class='LC20lb DKV0Md']")));
WebElement matchingRes1=driver.findElement(By.xpath("//h3[@class='LC20lb DKV0Md']"));
matchingRes1.getSize();
List<WebElement> listRes1=matchingRes.findElements(By.xpath("//h3[@class='LC20lb DKV0Md']"));
System.out.println(listRes1.size());
for(WebElement results1:listRes1)
{
String value = results1.getText();
System.out.println(value);
}
}
}
【问题讨论】:
标签: java selenium selenium-webdriver