1

I'm doing practise on this website: https://dojotoolkit.org/reference-guide/1.9/dijit/layout/TabContainer-examples.html

  1. Click on Run button in Programmatic Nested tabs section with following method:

        WebElement productElement = null;
        List<WebElement> productElements= driver.findElements(By.cssSelector(div.section));
    
        for(int i=0;i<productElements.size();i++)
        {
            String text = productElements.get(i).findElement(By.tagName("h2")).getText();
            if (text.equalsIgnoreCase(tabName)){
                productElement = productElements.get(i);
                break;
            }
        }
        return productElement;
    }
    public void clickRunButton(String tabName) {
        WebElement programmaticNestedtabs = findTab(tabName);
        WebElement runButton = programmaticNestedtabs.findElement(By.cssSelector("a.CodeGlassMiniRunner"));
        runButton.click();
    }
    
  2. The pop-up screen takes a while to load. Then I try to click on Tab 2:

WebDriverWait wait = new WebDriverWait(driver, 50);
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
dialog.findElement(By.xpath("//div[@class='dijitTabListWrapper dijitTabContainerTopNone dijitAlignCenter']//div[2]")).click();```

I got the StaleElementReferenceException when I run the code.


SerinaM
  • 21
  • 2

2 Answers2

0

StaleElementReferenceException is generally thrown when element is not yet attached to the DOM and you are trying to interact with it. You can get get around it by applying the wait condition where it waits until the element is ready to be clickable.

wait.until(ExpectedConditions.elementToBeClickable(WebElement));
Ankit
  • 13
  • 1
  • 5
0

I have written a general wrapper to handle similar exceptions in case of iFrame

private ExpectedCondition<Boolean> frameToBeAvailableAndSwitchToIt(final WebElement var0) {
    return new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver var1) {
            try {
                if (var0).isDisplayed()) {
                    var1.switchTo().frame(var0);
                    return true;
                }
            } catch (NoSuchFrameException var3) {
                return false;
            } catch (NoSuchElementException var4) {
                return false;
            } catch (StaleElementReferenceException var5) {
                return false;
            }
            return false;
        }

        public String toString() {
            return "frame to be available: " + var0;
        }
    };
}

You can call this as below

WebDriverWait wait = new WebDriverWait(driver, waitTimeOutInSeconds, pollTimeOutInMillis);
    wait.until(frameToBeAvailableAndSwitchToIt(iFrameWebElement));