首页 > 解决方案 > StaleElementReferenceException:过时的元素引用:元素未附加到页面文档(日历/日期选择器)

问题描述

我正在尝试单击箭头按钮,直到到达计划的出发月份,即 11 月,但单击后停止。我在 airfare.com 上成功使用了相同的方法,但它不适用于 expedia。它抛出错误“StaleElementReferenceException:过时的元素引用:元素未附加到页面文档”

有人可以帮忙吗?

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();


    }
}

标签: javaseleniumdatepickercalendar

解决方案


推荐阅读