java - 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();
}
}
解决方案
推荐阅读
- python - 构建图形时在 python 中使用下划线变量
- php - 从 python/PHP 打印
- javascript - 路由更改时重新安装组件
- c++ - 使用浮点键实现类似哈希表的数据结构,其中容差内的值被合并在一起
- java - 如何更正此代码以正确加载 ImageIcon
- python - asyncio/aiohttp - 如何进行一系列异步 - 但依赖 - 请求?
- android - Android - 启动时出现异常(对于某些用户)
- python - 如何从不同的进程访问全局变量
- javascript - IntersectionObserver.observe 不是对象,如何使用 IntersectionObserver
- python - 关于在pdf文档python中突出显示文本的问题