2016-12-15 48 views
0

我試圖從選擇的航班頁面中選擇航班,但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

StaleElementException如果我找到一個元素,在DOM被更新,然後我嘗試與element.f JavaScript會更新findElement呼叫和單擊呼叫然後我會得到一個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(); 



} 

}

+0

嗨Shubham,這很適合。非常感謝你。請你可以更多地瞭解你如何派生上面使用的xpath。 – Osa101

+0

Hi Shubham Mathur,我正在寫一個腳本,Selenium應該隨機點擊任何可用的單元格。請如何構建xpath以迎合所有可用的單元格? – Osa101

相關問題