Element not found in the cache | Selenium Forum
M
Posted on 14/12/2016
Hi Ashish,

i am trying to select a flight from the choose fligh page but Selenium is not able to locate any element on the page. I did try using different element locators but it still doesn't work. Can you help please?


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Cal_AA {
static WebDriver driver;

public static void main(String[] args) throws ParseException, InterruptedException {


driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.get("https://www.americanairlines.ie/intl/ie/index.jsp?locale=en_IE");

driver.findElement(By.xpath("//*[@id='bookingModule']/div[1]/div[1]/ul/li[2]/label/span[2]")).click();
driver.findElement(By.xpath("//*[@id='reservationFlightSearchForm.originAirport']")).sendKeys("LHR");
driver.findElement(By.xpath("//*[@id='reservationFlightSearchForm.destinationAirport']")).sendKeys("DFW");

driver.findElement(By.xpath("//*[@id='aa-leavingOn']")).click();
selectDate("12/06/2017");

}


public static void selectDate(String date) throws ParseException, InterruptedException{

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dateToBeSelected = df.parse(date);
Date currentDate = new Date();
String monthYearDisplayed = driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();

System.out.println("month year displayed " + monthYearDisplayed);
String month = new SimpleDateFormat("MMMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);
String day = new SimpleDateFormat("dd").format(dateToBeSelected);
String monthYearToBeSelected=month+ " "+year;
System.out.println(monthYearToBeSelected);


while(true){
if (monthYearToBeSelected.equals(monthYearDisplayed)) {
//select date
driver.findElement(By.xpath("//a[text()='"+day+"']")).click();
System.out.println("Found and Selected");
break;

}else{//if you are not in the right month & year, you have to then navigate to the right month & year

if(dateToBeSelected.after(currentDate)){


driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a")).click();//click fowardicon

}else{
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a")).click();//click backicon
}

}
monthYearDisplayed = driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();
}



driver.findElement(By.xpath("//*[@id='bookingModule-submit']")).click();

//driver.findElement(By.xpath("//*[@id='table-bound0-column0']")).click();
//driver.findElement(By.cssSelector("*[id='table-bound0-column0']")).click();

Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='tpl3_table-bound0-cell00-available']")).click();

//driver.findElement(By.cssSelector("*[id='tpl3_table-bound0-cell00-available']")).click();
//driver.findElement(By.id("tpl3_table-bound0-cell01-available")).click();

//WebDriverWait wait = new WebDriverWait(driver, 50);

//WebElement tpl3 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("tpl3_table-bound0-cell01-available")));
//tpl3.click();



}



}
















******************************************************error**************************************************************

log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
month year displayed December 2016
June 2017
Found and Selected
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
Command duration or timeout: 22.54 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'QJ07300112', ip: '10.67.127.123', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=38.5.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 105a82a9-c836-445c-a90e-c541eea86be0
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85)
at Cal_AA.selectDate(Cal_AA.java:80)
at Cal_AA.main(Cal_AA.java:30)
Caused by: org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:03:33'
System info: host: 'QJ07300112', ip: '10.67.127.123', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_111'
Driver info: driver.version: unknown
at <anonymous class>.fxdriver.cache.getElementAt(resource://fxdriver/modules/web-element-cache.js:9407)
at <anonymous class>.Utils.getElementAt(file:///C:/Users/642970/AppData/Local/Temp/anonymous8818508954300616830webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:8992)
at <anonymous class>.fxdriver.preconditions.visible(file:///C:/Users/642970/AppData/Local/Temp/anonymous8818508954300616830webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:10043)
at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/Users/642970/AppData/Local/Temp/anonymous8818508954300616830webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12597)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/642970/AppData/Local/Temp/anonymous8818508954300616830webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12614)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/642970/AppData/Local/Temp/anonymous8818508954300616830webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:623)

M
Replied on 18/12/2016

stale element reference exception could be solved by this
https://gist.github.com/djangofan/5112655


M
Replied on 20/12/2016

Thank you very much admin. I did get past huddle, although hudles keep coming.