4

The UI application that I am trying to automate has a horizontal menu, with a number of submenu options each (similar to a web browser toolbar).

I am trying to navigate to each sub menu option and verify that the page opens correctly. For IE, Firefox, and Chrome my code works correctly, but I am getting a lot of issues when I try to do it on Edge. The first few sub menu options open correct, but about half way through I am getting the following error:

org.openqa.selenium.StaleElementReferenceException: Stale element
reference (WARNING: The server did not provide any stacktrace
information) Command duration or timeout: 16 milliseconds For
documentation on this error, please visit:
http://seleniumhq.org/exceptions/stale_element_reference.html Build
info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30
17:32:46' System info: host: 'LPRDJMDB7R1', ip: '10.254.1.118',
os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0',
java.version: '1.8.0_101' Driver info:
org.openqa.selenium.edge.EdgeDriver Capabilities
[{applicationCacheEnabled=true, acceptSslCerts=true,
browserVersion=38.14393.0.0, platformVersion=10,
locationContextEnabled=true, webStorageEnabled=true,
browserName=MicrosoftEdge, takesScreenshot=true,
pageLoadStrategy=normal, takesElementScreenshot=true,
platformName=windows, platform=ANY}] Session ID:
3C5610CF-77F0-4EBD-ABA0-B09EE4778349

Followed by:

org.openqa.selenium.WebDriverException: Unknown error (WARNING: The
server did not provide any stacktrace information) Command duration or
timeout: 5 milliseconds Build info: version: '2.53.1', revision:
'a36b8b1', time: '2016-06-30 17:32:46' System info: host:
'LPRDJMDB7R1', ip: '10.254.1.118', os.name: 'Windows 10', os.arch:
'amd64', os.version: '10.0', java.version: '1.8.0_101' Driver info:
org.openqa.selenium.edge.EdgeDriver Capabilities
[{applicationCacheEnabled=true, acceptSslCerts=true,
browserVersion=38.14393.0.0, platformVersion=10,
locationContextEnabled=true, webStorageEnabled=true,
browserName=MicrosoftEdge, takesScreenshot=true,
pageLoadStrategy=normal, takesElementScreenshot=true,
platformName=windows, platform=ANY}] Session ID:
3C5610CF-77F0-4EBD-ABA0-B09EE4778349

And then my driver closes.

I've tried a number of different pieces of code to remove the StaleElementReferenceException (which did work for the other menu items), including:

new WebDriverWait(Setup.driver, 10)
.ignoring(StaleElementReferenceException.class).until(new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply(WebDriver driver) 
    {
        WebElement element = TestControl.findElementsByLinkTextScope(subMenuScope, subMenu);
        if(element!=null) 
        {
            errorOccurred= TestedElement.click(element);
            return true;
        }
        else
             return false;
    }
});

Any idea why this might be happening?

I used Selenium 2.53 and Edge WebDriver version 14393.

  • See my answer in your other question - Edge Driver simply isn't working as it should. Try posting this as a bug on the Edge Driver site, with some luck it might get fixed by 2019. – FDM Jun 6 '17 at 12:14
2

It happens when element is found but then changes before you click/interact with it.

It's hard to get around it even with explicit wait. Selenium/webdriver thinks element is present, visible, clickable even though it is still beeing updated and when you interact, it throws StaleElementReferenceException.

You can try catching error, and clicking again as a quick fix:

void Click(element){
 try {
  Find(element).Click()
 }
 catch (StaleElementReferenceException) {
  Click(element)
 }
1

I have seen "org.openqa.selenium.StaleElementReferenceException" in stack. Understand when this exception can be thrown:

  • When you are trying to interact with an element which is not available on screen/page StaleElement related exception will be thrown.

Solution to avoid this:

  1. It may happen that you are interacting with wrong element. Please cross check the element you are willing to interact

  2. Ensure that element is displayed on screen/page & then take appropriate action on it.

  3. Some time due to server response or internet there would be delay for that element to get load or display on screen. This can be avoid by using power of implicit wait

  4. For an urgency we have use explicit wait but this would not be the best practices. I won't recommend it to keep using always and everywhere

0

Try the below code and share you feedback.

public void clickUntillNotObsecured(WebElement elementToClick) {
    boolean obsecuredThrown = true;
    int c=0;
    while (obsecuredThrown && c<30) {
        try {
            clickOnElement(elementToClick);             
            obsecuredThrown = false;
        } catch (WebDriverException e) {
            obsecuredThrown = true;
        }
        c++;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.