i am learning selenium when i am executing following selenium program(selecting expected date from datepicker) i am getting stale element reference: element is not attached to the page document. I have gone through some online solutions but it is not working at the same time. I think while executing my program it is not finding the elements in DOM. Please guide me with the solution. Thanks in advance.
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.testng.annotations.Test;
public class skyscanner_datepicker
{
@Test
public void calender_func() throws InterruptedException
{
String expected_date = "January 2018";
String expected_day = "15";
System.setProperty("webdriver.chrome.driver", "D://rakesh//software//selenium browser//chrome 2.30//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.skyscanner.co.in/");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.findElement(By.id("js-depart-input")).click();
Thread.sleep(3000);
if(driver.findElement(By.xpath("//div[@class='popover skyscanner-datepicker depart']//span[@class='current']")).getText().equals(expected_date))
{
System.out.println("Date selected in if condition");
}
else
{
for(int i=1;i<=12;i++)
{
try
{
driver.findElement(By.xpath("//div[@class='popover skyscanner-datepicker depart']//div[@class='calendar-info-bar datepicker_clearfix']//button[2]")).click();
if(driver.findElement(By.xpath("//div[@class='popover skyscanner-datepicker depart']//span[@class='current']")).getText().equals(expected_date))
break;
System.out.println("date get selected");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
List<WebElement> day_list = driver.findElements(By.xpath("//div[@class='container-body']//tbody//tr//td"));
for(WebElement a : day_list)
{
if(a.getText().equalsIgnoreCase(expected_day))
{
a.click();
}
}
driver.quit();
}
}