Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=83.0.4103.106) For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/stale_element_reference.html Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {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, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true} Session ID: f9fd73fa86d306099bad2835337c25c0 *** Element info: {Using=xpath, value=//h3[@class='LC20lb DKV0Md']}
Results are getting displayed for the first page but when I click on second page results I get 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);
}
}
}