2015-05-12 80 views
1

Selenium:我必須從下拉菜單中選擇值,這取決於在另一個下拉菜單中選擇的值。Selenium:從下拉菜單中選擇一個值,該值取決於在另一個下拉菜單中選擇的值。

例如:我有兩個下拉菜單1和2.在2中填充的值取決於1.當我在下拉菜單1中選擇值時,頁面被刷新並且填充值2。我必須在下拉列表中選擇數值2.

我收到錯誤Element is no longer attached to DOM

我試過使用wait.until((ExpectedCondition<Boolean>) new ExpectedCondition<Boolean>()但它不幫助我。同樣的問題發生。

我嘗試使用WebElementSelect,但都沒有幫助。任何人都可以幫我找出解決方案嗎?

JavascriptExecutor executor2 = (JavascriptExecutor)driver; 
executor2.executeScript("arguments[0].click();", <elementname>); 
waitFor(3000); 

Select <objectname1>= new Select(driver.findElement(By.id("<ID_for_drop_down_1>"))); 
selectCourse.selectByVisibleText("<valuetobeselected>"); 
waitFor(2000); 

Select <objectname2>= new Select(driver.findElement(By.id("ID_for_drop_down_2"))); 
selectCourse.selectByVisibleText("<valuetobeselected>"); 
waitFor(2000); 

我正在使用waitFor(2000)定義的函數來等待指定的時間段。

+0

您可以添加代碼的工作示例嗎? – MeanGreen

+0

編輯帖子並添加示例代碼。 – Abhinav

+0

是否有可能以頁面源爲例,我們不能驗證是否有錯誤。 – Dude

回答

1

這些都是你需要的功能。這將對您有所幫助,這樣測試用例不會因測試過程中的頁面更改而失敗。選擇標籤的這種人口。

public void selectByValue(final By by, final String value){ 
    act(by, 3, new Callable<Boolean>() { 
     public Boolean call() { 
     Boolean found = Boolean.FALSE; 

     wait.until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(by))); 

     Select select = new Select(driver.findElement(by)); 

     select.selectByValue(value); 
     found = Boolean.TRUE; // FOUND IT 

     return found; 
     } 
    }); 
    } 

private void act(By by, int tryLimit, boolean mode, Callable<Boolean> method){ 

    boolean unfound = true; 
    int tries = 0; 
    while (unfound && tries < tryLimit) { 
     tries += 1; 
     try { 
     wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOfElementLocated(by))); 
     unfound = !method.call(); // FOUND IT, this is negated since it feel more intuitive if the call method returns true for success 
     } catch (StaleElementReferenceException ser) { 
     logger.error("ERROR: Stale element exception. "); 
     unfound = true; 
     } catch (NoSuchElementException nse) { 
     logger.error("ERROR: No such element exception. \nError: "+nse); 
     unfound = true; 
     } catch (Exception e) { 
     logger.error(e.getMessage()); 
     } 
    } 

    if(unfound) 
     Assert.assertTrue(false,"Failed to locate element"); 
    } 
+0

推薦的功能有什麼問題? –

1

Element no longer attached...如果刷新頁面並嘗試對先前創建的webElement對象執行任何操作,通常會出現該頁面。 此頁面可能會在選擇第一個下拉列表時刷新,看起來您正在對第一個下拉網頁元素執行選擇操作,而不是第二個。

Select dropDown1= new Select(driver.findElement(By.id("<ID_for_drop_down_1>"))); 
dropDown1.selectByVisibleText("<valuetobeselected>"); // Should be dropdown1 
waitFor(2000); 
// Page might be refreshed here 
Select dropDown2= new Select(driver.findElement(By.id("ID_for_drop_down_2"))); 
dropDown2.selectByVisibleText("<valuetobeselected>"); // use dropdwon2 not dropdown1 

有關詳細信息:Random "Element is no longer attached to the DOM" StaleElementReferenceException

相關問題