在缓存中找不到的网页元素中未识别的元素 - 也许该页面自查找以来已经发生了变化



我正在尝试从"选择飞行"页面中选择一个航班,但硒无法在页面上找到任何元素。我确实尝试使用不同的元素定位器,但仍然不起作用。你能帮忙吗?

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

} 

}

staleelementException发生如果我找到一个元素,dom将更新,然后尝试与元素进行交互。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();

} 

}

最新更新