StaleElementReferenceException: stale element reference: element is not attached to the page document (Calendar/DatePicker)

huangapple 未分类评论43阅读模式
英文:

StaleElementReferenceException: stale element reference: element is not attached to the page document (Calendar/DatePicker)

问题

package com.w2a.rough;

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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExpediaDatePicker {
    public static void main(String[] args) throws InterruptedException {

        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        WebDriverWait wait = new WebDriverWait(driver, 15);
        driver.get("https://www.expedia.com");

        // Click on the flight tab
        driver.findElement(By.cssSelector("#uitk-tabs-button-container > li:nth-child(2) > a")).click();

        // Click on Departing field to open the calendar
        driver.findElement(By.cssSelector("#d1-btn")).click();

        // Planned departure month and date
        String DepartMonth = "November 2020";
        String DepartDate = "15";

        // Navigate through the months to get to the planned departure month.
        WebElement defaultMonth = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='uitk-new-date-picker-desktop-months-container']//div[1]/h2")));
        System.out.println(defaultMonth.getText());

        while (true) {
            if (defaultMonth.getText().equals(DepartMonth)) {
                break;
            } else {
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='uitk-calendar']//button[2]"))).click();
            }
        }
        // Get the list of departure dates.
        List<WebElement> depDates = driver.findElements(By.xpath("//div[@class='uitk-calendar']//div[1]//table[1]//tbody[1]//td"));

        // Loop through the list and click on the intended departure date
        for (int i = 0; i < depDates.size(); i++) {
            if (depDates.get(i).getText().equals(DepartDate)) {
                depDates.get(i).click();
            }
        }
        // Click on the Done button.
        driver.findElement(By.cssSelector("button[data-stid='apply-date-picker']")).click();

        // Click on the return date field.
        driver.findElement(By.cssSelector("#d2-btn")).click();

        // Planned return month and date.
        String returnMonth = "November 2020";
        String returnDate = "22";

        // Navigate through the months to get to the desired return month.
        WebElement currentMonth = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='uitk-new-date-picker-desktop-months-container']//div[1]/h2")));

        while (true) {
            if (currentMonth.getText().equals(returnMonth)) {
                break;
            } else {
                driver.findElement(By.xpath("//div[@class='uitk-calendar']//button[2]")).click();
                Thread.sleep(1000);
            }
        }
        // List the return dates
        List<WebElement> rtnDates = driver.findElements(By.xpath("//div[@class='uitk-calendar']//div[1]//table[1]//tbody[1]//td"));
        // Now click on the planned return date date.
        for (int j = 0; j < rtnDates.size(); j++) {
            if (rtnDates.get(j).getText().equals(returnDate)) {
                rtnDates.get(j).click();
            }
        }

        // Click on the Done button.
        driver.findElement(By.cssSelector("button[data-stid='apply-date-picker']")).click();
    }
}
英文:

I am trying to click on the arrow button until I reach to the planned departure month, which is November but it stops after one click. I used the same approach on airfare.com successfully but it's not working on expedia. It's throwing the error "StaleElementReferenceException: stale element reference: element is not attached to the page document"

Could anyone, please, help?

package com.w2a.rough;

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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExpediaDatePicker {
	public static void main(String[] args) throws InterruptedException {

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().deleteAllCookies();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		WebDriverWait wait = new WebDriverWait(driver, 15);		
		driver.get(&quot;https://www.expedia.com&quot;);

       //Click on the flight tab		

		driver.findElement(By.cssSelector(&quot;#uitk-tabs-button-container &gt; li:nth-child(2) &gt; a&quot;)).click();
		


		//Click on Departing field to open the calendar
		driver.findElement(By.cssSelector(&quot;#d1-btn&quot;)).click();

		//Planned departure month and date

		String DepartMonth = &quot;November 2020&quot;;
		String DepartDate = &quot;15&quot;;
       
       //Navigate through the months to get to the planned departure month.

		WebElement defaultMonth = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[@class=&#39;uitk-new-date-picker-desktop-months-container&#39;]//div[1]/h2&quot;)));
		System.out.println(defaultMonth.getText());
		
		while(true) {
			if(defaultMonth.getText().equals(DepartMonth)) {
				break;
			}else {
				wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[@class=&#39;uitk-calendar&#39;]//button[2]&quot;))).click();

			}
		}
		//Get the list of departure dates.
		List&lt;WebElement&gt; depDates = driver.findElements(By.xpath(&quot;//div[@class=&#39;uitk-calendar&#39;]//div[1]//table[1]//tbody[1]//td&quot;));

		//Loop through the list and click on the intended departure date
		for(int i = 0; i &lt; depDates.size(); i++) {
			if(depDates.get(i).getText().equals(DepartDate)){
				depDates.get(i).click();
			}
		}
		//Click on the Done button.
		driver.findElement(By.cssSelector(&quot;button[data-stid=&#39;apply-date-picker&#39;]&quot;)).click();

		//Click on the return date field.
		driver.findElement(By.cssSelector(&quot;#d2-btn&quot;)).click();
		
		//Planned return month and date.

		String returnMonth = &quot;November 2020&quot;;
		String returnDate = &quot;22&quot;;
		
		//Navigate through the months to get to the desired return month.

		WebElement currentMonth = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//div[@class=&#39;uitk-new-date-picker-desktop-months-container&#39;]//div[1]/h2&quot;)));
		
		while(true) {
			if(currentMonth.getText().equals(returnMonth)) {
				break;
			}else {
				driver.findElement(By.xpath(&quot;//div[@class=&#39;uitk-calendar&#39;]//button[2]&quot;)).click();
				Thread.sleep(1000);
			}
		}
        //List the return dates
		List&lt;WebElement&gt; rtnDates =	driver.findElements(By.xpath(&quot;//div[@class=&#39;uitk-calendar&#39;]//div[1]//table[1]//tbody[1]//td&quot;));
		//Now click on the planned return date date.
		for(int j = 0; j &lt; rtnDates.size(); j++) {
			if(rtnDates.get(j).getText().equals(returnDate)){
				rtnDates.get(j).click();
			}
		}

		//Click on the Done button.
		driver.findElement(By.cssSelector(&quot;button[data-stid=&#39;apply-date-picker&#39;]&quot;)).click();


	}
}


</details>


huangapple
  • 本文由 发表于 2020年7月25日 22:58:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/63089830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定