Sumit Goyal
Sumit Goyal

Reputation: 1

Selenium Calender UI Handling

Iam learning calender UI handling concept in selenium but stuck Code 1 :

import java.util.concurrent.TimeUnit;

enter code here

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class CalenderUI {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\sumit goyal\\Downloads\\chromedriver_latest\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://www.airindia.in/");
        driver.findElement(By.cssSelector("li.bookFlight")).click(); //CALENDER Resides inside it

        driver.findElement(By.xpath("//input[@title='Departing']")).click(); // will open calendar POPUPmenu

        WebElement cal = driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/div[2]")); //restricting scope

        while(!(driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] span:nth-child(2)")).getText().equals("2021")))

        {
            cal.findElement(By.cssSelector("a[data-handler='next']")).click(); //will click on next button until 2021 arrives

        }

    }

}

after running code i got this error : stale element reference: element is not attached to the page document

Every Selector seems correct I checked it iam not getting why its failing

Upvotes: 0

Views: 37

Answers (1)

NarendraR
NarendraR

Reputation: 7708

This can happen if a DOM operation happening on the page is temporarily causing the element to be inaccessible. To allow for those cases, you need try to re locate the element again inside the loop.

Replace below code :

driver.findElement(By.xpath("//input[@title='Departing']")).click(); // will open calendar POPUPmenu
while (!(driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] span:nth-child(2)")).getText().equals("2021"))) {
    WebElement cal = driver.findElement(By.xpath("//div[@id='ui-datepicker-div']/div[2]")); //restricting scope
    cal.findElement(By.cssSelector("a[data-handler='next']")).click(); //will click on next button until 2021 arrives
}

Upvotes: 1

Related Questions