0

我正在用4個選擇測試一個表單。當我選擇第一個時,它會根據值進行ajax調用並填充下一個選擇。但大部分時間我都會遇到StaleElementReferenceException。 I'm實施FluentWait和我的自定義預期的測試狀態的選擇是使用相同的FluentWaitajax選項上的Selenium StaleElementReferenceException選項

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(webdriver).withTimeout(10, TimeUnit.SECONDS) 
       .pollingEvery(2, TimeUnit.MILLISECONDS) 
       .ignoring(StaleElementReferenceException.class) 
       .ignoring(NoSuchElementException.class); 

下面是檢索webelement代碼填充

I'm,並選擇第三個元素。

 WebElement brand= wait.until(ExpectedConditions.elementToBeClickable(By.id("brand"))); 
     Select selectBrand= new Select(brand); 
     wait.until(new SelectCondition(selectBrand));   
     marcaSelect.selectByIndex(2); 


     WebElement model= wait.until(ExpectedConditions.elementToBeClickable(By.id("model"))); 
     Select modelSelect = new Select(model); 
     wait.until(new SelectCondition(modelSelect)); 
     modeloSelect.selectByIndex(2); 

我的定製預期條件

class SelectCondition implements ExpectedCondition{ 

     Select select ; 

     public SelectCondition(Select select){ 
      this.select = select; 
     } 

     public Boolean apply(Object arg0) { 
      return this.select.getOptions().size() > 1 ; 
     } 

    } 

硒林webdriver的測試3.3.1,JSF 2.2,JAVA 1.7,鉻,日食

+0

哪個特定的線路給你StaleEelementException? – kushal

+0

return this.select.getOptions()。size()> 1; – jckhan

回答

0
// Below method will wait till value you are expecting is displayed in dropdown 
public void waitForDropDownToLoad(final By by, final String sVal, 
     String iSecs) { 
    try { 

     Wait<WebDriver> wait = new FluentWait<WebDriver>(getDriver()) 
       .withTimeout(Integer.parseInt(iSecs), TimeUnit.SECONDS) 
       .pollingEvery(
         Integer.parseInt(configProperties 
           .getProperty("sPollingTime")), 
         TimeUnit.SECONDS) 
       .ignoring(NoSuchElementException.class) 
       .ignoring(StaleElementReferenceException.class); 
     wait.until(new ExpectedCondition<Boolean>() { 
      public Boolean apply(WebDriver driver) { 
       Select select = new Select(getDriver().findElement(by)); 
       List<WebElement> options = select.getOptions(); 
       for (WebElement option : options) { 
        if (option.getText().equals(sVal)) { 
         return true; 
        } 
       } 
       return null; 

      } 
     }); 
    } catch (Exception e) { 
     log.info("Exception in waitForDropDownToLoad()" + e); 
    } 
} 

//Now try below code 
WebElement brand= 
wait.until(ExpectedConditions.elementToBeClickable(By.id("brand"))); 

//Wait for option to be loaded in dropdown 
waitForDropDownToLoad(By.id("brand"), "OptionText", "10") 

// Select option in dropdown 
Select selectBrand= new Select(brand); 
selectBrand.selectByVisibleText("OptionText"); 

// Get webelement 
WebElement model= 
wait.until(ExpectedConditions.elementToBeClickable(By.id("model"))); 

//Wait for option to be loaded in dropdown 
waitForDropDownToLoad(By.id("model"), "ModelOption", "10") 

//Select option 
Select modelSelect = new Select(model); 
modeloSelect.selectByVisibleText("ModelOption"); 
+0

我使用這種方法,但仍然有Select element = new Select(element)上的StaleReferenceException; – jckhan