英文:
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("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();
}
}
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论