尽管有20次重试,陈旧元素异常仍然存在

时间:2017-03-30 10:19:44

标签: java selenium staleobjectstateexception

我之前看过一个关于陈旧元素异常的帖子,并使用重试代码来处理它。但是,尽管将计数保持在20,但过时的元素异常仍然存在。我可以看到element2被加载到正在测试的网页中。但它仍然是作为陈旧元素的id。代码有时适用于element1。但从来没有对于element2 代码:

for (i = 1; i < 7; i++)
        {   
            sServiceID = ExcelUtils.getCellData(i,Constant.Col_ServiceID);
            System.out.println("sServiceID:"+sServiceID);   

            ServiceID_Filter().clear();//function returns element
            ServiceID_Filter().sendKeys(sServiceID);
            BaseClass.driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

            Thread.sleep(3000);
            ApplyFilters_element().click();
            Thread.sleep(3000);


      boolean result = false;
                        int attempts = 0;
                        while(attempts < 20) {
                            System.out.println("inside stale check loop");
                            BaseClass.driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
                            try {
                                if(element1.isDisplayed()||element2.isDisplayed())  //either one of the elements will be loaded
                                {
                                System.out.println("not stale "+Table_widget.ExportButton().isDisplayed());
                                result = true;
                                break;
                                }
                            } catch(StaleElementReferenceException e) {
                                System.out.println("stale at attempt "+attempts);
                            }
                            attempts++;
                        }
    if(result==true)
                  {
                      if(element1.isDisplayed())
                      {
                          element3.click();  
                          System.out.println(" button clicked");
                          Thread.sleep(1000);
                      }
                      else
                          if(element2.isDisplayed())
                          {  element3.click();  
                             System.out.println("No records found"); 
                             Thread.sleep(1000);
                          }
                  }
        }

1 个答案:

答案 0 :(得分:1)

我的拙见认为问题在于:

 BaseClass.driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

            Thread.sleep(3000);
            ApplyFilters_element().click();
            Thread.sleep(3000);

首先,您使用隐式等待加线程休眠,这是灾难的一个方法。这就是造成陈旧元素异常的原因,请尝试以下内容:

public boolean waitForElement(String elementXpath, int timeOut) {

    try{                    
    WebDriverWait wait = new WebDriverWait(driver, timeOut); 
    boolean elementPresent=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementXpath)).isDisplayed());

    System.out.printf("%nElement is present [T/F]..? ")+elementPresent;
    }        
    catch(TimeoutException e1){e1.printStackTrace();elementPresent=false;}          

    return elementPresent;   
 }

祝你好运!