The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed.
Hi, I am trying to select a start date and an end date from a calendar dropdown control. Based on my code, its supposed to select 23rd and 30th dates respectively. Once it selects 23rd it throws and exception below:
org.openqa.selenium.StaleElementReferenceException: The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed.
I tried using thread.sleep and wait.until(ExpectedConditions.visibilityOfElementLocated((By.xpath("xpath here")))) but nothing seems to work. Below is the code which I have written. It will be of great help if you can provide a solution. Thanks.
I am using selenium 3.3.1 and geckodriver v0.15.0-win64.
List
List
int total_end_dates = end_dates.size();
int total_start_dates = start_dates.size();
for (int j = 0;j <total_start_dates;j++)
{
String start_date = start_dates.get(j).getText();
String start_name = start_dates.get(j).getAttribute("class");
if(start_date.equals("23") & start_name.equals("available"))
{
start_dates.get(j).click();
break;
}
}
for (int i = 0;i <total_end_dates;i++){
String date = end_dates.get(i).getText();
String name = end_dates.get(i).getAttribute("class");
if(date.equals("30") & name.equals("available"))
{
end_dates.get(i).click();
break;
}
}
I have encountered similar issues to this, in all likelihood you have stale references. So this isn't an issue.
In datepickers/calendars. When you select one date, the entire DOM is updated. This will mean all your previous references are almost certainly wrong.
The way to get around this is have method a for selecting date 1, then method b (Which rescrapes), to select date 2
Hi,
Can you please share a sample code if you have on how to tackle this issue. That will help immensely. Thanks.
Thanks so much. Your suggestion worked. Closing this thread.
i am facing the same problem , can anyone explain how to scrape such forms. i did not get what luke hill meant by "The way to get around this is have method a for selecting date 1, then method b (Which rescrapes), to select date 2" , rescrape. i searched but getting the same error.
Essentially when you pick your first date. You then need to re-obtain all elements by doing #find and then click date 2
sorry but i am still unable to get it. i have three drop_downs and [d1,d2,d3] and their select is not independent , when i select an option from d1 then i am given some particular options in d2 . like selecting country then state then province. what should i do to understand it?
after getting a list of options from 1st select but when i am making that submit , i am getting dom is stale .
If you have any snippet of code where the fail is shown, share it with us. So, it will be easier to help you.
This has been resolved , just needed to reload/refresh Dom and then had to look for the element . Thanks