簡體   English   中英

Selenium WebDriver中的陳舊元素異常

[英]Stale element Exception in selenium webdriver

我試圖自動執行一個方案,其中的條件是必須選擇所有下拉選項,並且必須逐個單擊該選項。 我嘗試使用代碼,但它僅單擊第一個選項。並且顯示錯誤為陳舊元素不可單擊。 請幫忙。

如果找到元素,則發生StaleElementException ,DOM被更新,然后我嘗試與該元素進行交互。

那么我該如何處理呢? 使用以下單擊方法嘗試多次單擊元素:

public boolean retryingFindClick(By by) {
        boolean result = false;
        int attempts = 0;
        while(attempts < 2) {
            try {
                driver.findElement(by).click();
                result = true;
                break;
            } catch(StaleElementException e) {
            }
            attempts++;
        }
        return result;
}

這里采取了這個出色的解決方案。

當您從下拉列表中選擇一個選項時,您的DOM也將得到更新。 因此,您也需要更新對象。 請參見示例代碼段:

    Select select = new Select(driver.findElement(By.cssSelector("your dropdown's locator"))); // you may use any locator of your choice
    List<WebElement> options = select.getOptions();

    for(WebElement option : options){
            select.selectByVisibleText(option.getText());

            //re-assign your select object since your page has been reloaded after selecting an option 
            select =   new Select(driver.findElement(By.cssSelector("your dropdown's locator")));
     }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM