user3174553
user3174553

Reputation: 69

StaleElementReferenceException: stale element reference:

I need to access the folderId's from the code:

{
"folderType": 3,
            "createdDate": "02.09.2014 11:57:28.UTC",
            "modifiedDate": "02.09.2014 11:57:28.UTC",
            "folderName": "Enterprise",
            "folderId": "baebc0fb-38cb-4300-9bd1-9b98fa622e4a",
            "widgetType": null,
            "enterpriseId": "366fc1f1-670d-41bc-905e-bc80accd2537",
            "parentFolderId": "",
}
{
"folderType": 3,
            "createdDate": "02.09.2014 11:57:28.UTC",
            "modifiedDate": "02.09.2014 11:57:28.UTC",
            "folderName": "Enter",
            "folderId": "6671ca49-9637-488e-9a0e-f7dbf415a542",
            "widgetType": null,
            "enterpriseId": "366fc1f1-670d-41bc-905e-bc80accd2537",
            "parentFolderId": "",
}

I need to access the folderId of 'Enterprise' and 'Enter' folder.

This is my code:

String s1= driver.findElement(By.xpath("//span[. = '\"Enterprise\"']/ancestor::pre/following-sibling::pre[1]/span[2]")).getText();

String s2=driver.findElement(By.xpath("//span[. = '\"Enter\"']/ancestor::pre/following-sibling::pre[1]/span[2]")).getText();

I want to print the values of String s1 and s2 without the cotes. I can access the 'Enterprise' folderId but unable to access the 'Enter' folderId. As 'Enter' folderId is newly created folder.

For every field present between the cotes, following is the GENERAL FORAMT HTML code:

<pre>            <span class="cm-string">"folderName"</span>: <span class="cm-string">"Enter"</span>,</pre>
<pre>            <span class="cm-string">"folderId"</span>: <span class="cm-string">"6671ca49-9637-488e-9a0e-f7dbf415a542"</span>,</pre>

I can select the Enter folderId using the chrome Developer Tools like this:

$x("//span[. = '\"Enter\"']/ancestor::pre/following-sibling::pre[1]/span[2]")
[<span class=​"cm-string">​"6671ca49-9637-488e-9a0e-f7dbf415a542"​</span>​]

Actual Output in the eclipse: Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=37.0.2062.124)

Upvotes: 0

Views: 662

Answers (1)

Braj
Braj

Reputation: 46841

StaleElementReferenceException is due to unavailability of an element being accessed by findelement() method.

Use below sample code to wait for element's visibility:

private static WebElement findElement(WebDriver driver, By locator) {
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(90, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class, StaleElementReferenceException.class);
    return wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}

Read more...

Upvotes: 2

Related Questions