我正在尝试从选择航班页面中选择航班,但Selenium无法在页面上找到任何元素。我尝试使用不同的元素定位器,但它仍然无法正常工作。你能帮忙吗?
public class Cal_AA {
static WebDriver driver;
public static void main(String[] args) throws ParseException, InterruptedException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.get("https://www.americanairlines.ie/intl/ie/index.jsp?locale=en_IE");
driver.findElement(By.xpath("//*[@id='bookingModule']/div[1]/div[1]/ul/li[2]/label/span[2]")).click();
driver.findElement(By.xpath("//*[@id='reservationFlightSearchForm.originAirport']")).sendKeys("LHR");
driver.findElement(By.xpath("//*[@id='reservationFlightSearchForm.destinationAirport']")).sendKeys("DFW");
driver.findElement(By.xpath("//*[@id='aa-leavingOn']")).click();
selectDate("12/06/2017");
}
public static void selectDate(String date) throws ParseException, InterruptedException{
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dateToBeSelected = df.parse(date);
Date currentDate = new Date();
String monthYearDisplayed = driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();
System.out.println("month year displayed " + monthYearDisplayed);
String month = new SimpleDateFormat("MMMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);
String day = new SimpleDateFormat("dd").format(dateToBeSelected);
String monthYearToBeSelected=month+ " "+year;
System.out.println(monthYearToBeSelected);
while(true){
if (monthYearToBeSelected.equals(monthYearDisplayed)) {
//select date
driver.findElement(By.xpath("//a[text()='"+day+"']")).click();
System.out.println("Found and Selected");
break;
}else{//if you are not in the right month & year, you have to then navigate to the right month & year
if(dateToBeSelected.after(currentDate)){
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a")).click();//click fowardicon
}else{
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a")).click();//click backicon
}
}
monthYearDisplayed = driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();
}
driver.findElement(By.xpath("//*[@id='bookingModule-submit']")).click();
//driver.findElement(By.xpath("//*[@id='table-bound0-column0']")).click();
//driver.findElement(By.cssSelector("*[id='table-bound0-column0']")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='tpl3_table-bound0-cell00-available']")).click();
//driver.findElement(By.cssSelector("*[id='tpl3_table-bound0-cell00-available']")).click();
//driver.findElement(By.id("tpl3_table-bound0-cell01-available")).click();
//WebDriverWait wait = new WebDriverWait(driver, 50);
//WebElement tpl3 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("tpl3_table-bound0-cell01-available")));
//tpl3.click();
}
}
答案 0 :(得分:0)
如果我找到一个元素,那么会发生StaleElementException,DOM会更新然后我尝试与element.f进行交互Javascript更新findElement调用和click调用之间的页面然后我将得到一个StaleElementException。在现代网页上发生这种情况并不罕见。然而,它不会持续发生。时机必须适合发生此错误。
以下代码适合您
公共课Cal_AA {
static WebDriver driver;
public static void main(String[] args) throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.get("https://www.americanairlines.ie/intl/ie/index.jsp?locale=en_IE");
driver.findElement(By.xpath("//*[@id='bookingModule']/div[1]/div[1]/ul/li[2]/label/span[2]")).click();
driver.findElement(By.xpath("//*[@id='reservationFlightSearchForm.originAirport']")).sendKeys("LHR");
driver.findElement(By.xpath("//*[@id='reservationFlightSearchForm.destinationAirport']")).sendKeys("DFW");
driver.findElement(By.xpath("//*[@id='aa-leavingOn']")).click();
selectDate("12/06/2017");
}
public static void selectDate(String date) throws Exception{
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dateToBeSelected = df.parse(date);
Date currentDate = new Date();
String monthYearDisplayed = driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();
System.out.println("month year displayed " + monthYearDisplayed);
String month = new SimpleDateFormat("MMMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);
String day = new SimpleDateFormat("dd").format(dateToBeSelected);
String monthYearToBeSelected=month+ " "+year;
System.out.println(monthYearToBeSelected);
while(true){
if (monthYearToBeSelected.equals(monthYearDisplayed)) {
//select date
driver.findElement(By.xpath("//a[text()='"+day+"']")).click();
System.out.println("Found and Selected");
break;
}else{//if you are not in the right month & year, you have to then navigate to the right month & year
if(dateToBeSelected.after(currentDate)){
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a")).click();//click fowardicon
}else{
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a")).click();//click backicon
}
}
monthYearDisplayed = driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();
}
driver.findElement(By.xpath("//*[@id='bookingModule-submit']")).click();
//driver.findElement(By.xpath("//*[@id='table-bound0-column0']")).click();
//driver.findElement(By.cssSelector("*[id='table-bound0-column0']")).click();
Thread.sleep(20000);
int attempts = 0;
while(attempts < 4) {
try {
driver.findElement(By.xpath("//div[@class='availability-container row']/descendant::div[contains(@id,'table-bound0-cell00-available-content')][1]")).click();
System.out.println("CLICKED !!");
break;
} catch(Exception e) {
System.out.println("EXCEPTION OCCURED..RETRY !!");
}
attempts++;
}
//driver.findElement(By.cssSelector("*[id='tpl3_table-bound0-cell00-available']")).click();
//driver.findElement(By.id("tpl3_table-bound0-cell01-available")).click();
//WebDriverWait wait = new WebDriverWait(driver, 50);
//WebElement tpl3 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("tpl3_table-bound0-cell01-available")));
//tpl3.click();
}
}