selenium icon indicating copy to clipboard operation
selenium copied to clipboard

The element reference is stale. Either the element is no longer attached to the DOM or the page has been refreshed.

Open rajarshisaha opened this issue 9 years ago • 9 comments

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 start_dates = web.findElements(By.xpath("//div[@class='daterangepicker dropdown-menu opensright']/div[2]/div[1]/table[1]//td"));

List end_dates = web.findElements(By.xpath("//div[@class='daterangepicker dropdown-menu opensright']/div[1]/div[1]/table[1]//td"));

		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;
			}
		}

rajarshisaha avatar Apr 19 '17 10:04 rajarshisaha

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

luke-hill avatar Apr 19 '17 10:04 luke-hill

Hi,

Can you please share a sample code if you have on how to tackle this issue. That will help immensely. Thanks.

rajarshisaha avatar Apr 19 '17 11:04 rajarshisaha

Thanks so much. Your suggestion worked. Closing this thread.

rajarshisaha avatar Apr 19 '17 11:04 rajarshisaha

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.

aryan-solo avatar Mar 07 '18 14:03 aryan-solo

Essentially when you pick your first date. You then need to re-obtain all elements by doing #find and then click date 2

luke-hill avatar Mar 07 '18 14:03 luke-hill

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?

aryan-solo avatar Mar 07 '18 16:03 aryan-solo

after getting a list of options from 1st select but when i am making that submit , i am getting dom is stale .

aryan-solo avatar Mar 07 '18 16:03 aryan-solo

If you have any snippet of code where the fail is shown, share it with us. So, it will be easier to help you.

danielplaku avatar Jun 01 '18 07:06 danielplaku

This has been resolved , just needed to reload/refresh Dom and then had to look for the element . Thanks

aryan-solo avatar Jun 01 '18 07:06 aryan-solo