0

I have to iterate List through foreach loop and need to click on item. First item of the list is getting clicked but from second item onwards i am getting

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

public void checkFlightAvailabilityToSelectOutBound() throws InterruptedException {

    boolean enabledFound=BookDateFlight();
    System.out.println("checkFlightAvailabilityToSelectOutBound  "+enabledFound);

    if(enabledFound==false)
    {
        List<WebElement> nextAvailableDateList = driver
                .findElements(By.xpath("somexpath"));
        System.out.println("date list lenth "+nextAvailableDateList.size());
        **for (WebElement nxtAvlDate : nextAvailableDateList) 
        {
            try {
            System.out.println("------"+nxtAvlDate);
            System.out.println("Trying to click on the nxt avl date "+nxtAvlDate.getAttribute("id"));
            //wait.until(ExpectedConditions.elementToBeClickable(nxtAvlDate));
            //driver.navigate().refresh();
            Thread.sleep(3000);
            nxtAvlDate.click();
            Thread.sleep(5000);**
            //wait.until(ExpectedConditions.visibilityOfAllElements(flightInfoOutBoundTravelClassBtnList));
            }catch(Exception e) {
                e.printStackTrace();
                System.out.println("message catch"+e.getMessage());

            }

            enabledFound=BookDateFlight();
            System.out.println("After clicking nxt avil date "+enabledFound);
            if(enabledFound==true)
            {
                break;
            }
        }
        if(enabledFound==false)
        {
            System.out.println("inside next 7 day");
            driver.findElement(By.xpath("//p[@class='next']")).click();
            System.out.println("inside next 7 day clicked");
            Thread.sleep(10000);
            checkFlightAvailabilityToSelectOutBound();

            }

    }

}
SiKing
  • 10,003
  • 10
  • 39
  • 90

2 Answers2

0

In case of staleelementexception the problem is generally due to the fact that a reference is requested to an element that has been updated or is new (even if maybe it has the same id) and therefore we have an invalid reference to it. It can be solved by asking for a clean reference of the item or adding a wait in the workflow

You can try something like this:

WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,timeout)
                  .ignoring(StaleElementReferenceException.class); 
  wait.until(new ExpectedCondition<Boolean>(){ 
    @Override 
    public Boolean apply(WebDriver webDriver) { 
      WebElement element = webDriver.findElement(by); 
      return element != null && element.isDisplayed(); 
    } 
  }); 

Adapting it to you code

Paolo Mossini
  • 1,064
  • 2
  • 15
  • 23
0

it usually occurs when the dom is getting refreshed .try the following explicit wait before doing the click

wait.until(ExpectedConditions.stalenessOf(nxtAvlDate));
Alexander Hoffmann
  • 5,104
  • 3
  • 17
  • 23
Riswan chatholi
  • 149
  • 1
  • 4