0

I am trying to automate a calendar with Selenium WebDriver Java. When it comes to identify the date and click the date, it throws a StaleElementRefernceExeception. Please see the code below and help me to fix it.

package com.initial.selenium;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
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 Calender2 {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Shahid\\eclipseworkspace\\InitialSeleniumProject\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.path2usa.com/travel-companions");
        driver.findElement(By.xpath("//*[@id='travel_from']")).sendKeys("Chicago");
        driver.findElement(By.xpath("//*[@id='Rockford']")).click();
        driver.findElement(By.xpath("//*[@id='travel_to']")).sendKeys("Dallas");
        driver.findElement(By.xpath("//*[@id='Dallas-Fort Worth']")).click();
        driver.findElement(By.xpath("//*[@id='travel_date']")).click();

        WebElement DateMonth=driver.findElement(By.xpath("//div[@class='datepicker- days']/table/thead/tr/th[2]"));
        WebElement datechanger=driver.findElement(By.xpath("//div[@class='datepicker- days']/table/thead/tr/th[3]"));
        List<WebElement> dates=driver.findElements(By.tagName("td"));
        while(!DateMonth.getText().contains("April")) {
            datechanger.click();
            for(int i=0;i<dates.size();i++) {
                if(dates.get(i).getText().equalsIgnoreCase("23")) {
                    dates.get(i).click();  
                    break;
                }   
            }
        }
    }

}
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • Please review your question as there are a couple formatting/typo issues. Can you also share the full error text, which would likely indicate where the `StaleElementReferenceException` is thrown? – C. Peck Mar 21 '19 at 18:05
  • When writing automation tests, generally there's no need to emulate selecting a date in a datepicker through the drop-down unless you're testing the datepicker itself. In the vast majority of cases, identifying the datepicker itself as a webelement and doing a sendKeys of the date to it is sufficient. Most automation scripts are to test a process, a series of steps, not necessarily a one-to-one click of things like dialog boxes that can be avoided. – Bill Hileman Mar 21 '19 at 18:19
  • The xpath that you were using to change the month was incorrect, i have updated my answer and have added the xpath for it. You can check it and let me know if that helps. – Sameer Arora Mar 22 '19 at 04:27

1 Answers1

0

StaleElementReferenceException comes when the element you are trying to operate on is no longer present in the html or has become stale. So, to correct it, you have to fetch the elements again.

In your case, you can correct it by making the below changes in your while loop:

List<WebElement> dates=driver.findElements(By.tagName("td"));
while (!DateMonth.getText().contains("April")) {
    datechanger.click();
    dates=driver.findElements(By.tagName("td"));
    for (int i = 0; i < dates.size(); i++) {
        if (dates.get(i).getText().equalsIgnoreCase("23")) {
            dates.get(i).click();
            break;
        }
    }
}

Also, the xpath you are using to change is the month is incorrect, so use the given below xpath instead of the one you are using:

WebElement datechanger=driver.findElement(By.xpath("(//th[@class='prev'])[1]"));
Sameer Arora
  • 4,439
  • 3
  • 10
  • 20