StaleElementReference Exception in PageFactory

后端 未结 3 1549
眼角桃花
眼角桃花 2020-11-21 05:17

I am trying to learn the PageFactory model. I understood the fact that when we do a initElements, the WebElements are located. Say for example, I click on a web

3条回答
  •  感情败类
    2020-11-21 05:47

    Stale element exception is thrown in two cases

    The element is no longer attached to the DOM. The element has been deleted entirely.

    When this happen you wrap your code in try catch block then you can loop and retry as many times as you need until it succeeds.

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

提交回复
热议问题