1
org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM

Open 1 Answers 45 Views
org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM
Command duration or timeout: 11.40 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'DESKTOP-B8PHOC3', ip: '169.254.80.99', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: b97e0a8b-7bc2-4ab6-9a56-3f70cb1f8191
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:577)
at pageObjects.LatestArticles.clickAroLoadKorunLink(LatestArticles.java:59)
at testCases.VerifyLatestArticles.verifyLoadLatestArticles(VerifyLatestArticles.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
Caused by: org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'DESKTOP-B8PHOC3', ip: '169.254.80.99', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111'
Driver info: driver.version: unknown
at <anonymous class>.fxdriver.cache.getElementAt(resource://fxdriver/modules/web-element-cache.js:9457)
at <anonymous class>.Utils.getElementAt(file:///C:/Users/mehed/AppData/Local/Temp/anonymous2841595966541034444webdriver-profile/extensions/[email protected]/components/driver-component.js:9039)
at <anonymous class>.Utils.unwrapParameters(file:///C:/Users/mehed/AppData/Local/Temp/anonymous2841595966541034444webdriver-profile/extensions/[email protected]/components/driver-component.js:9262)
at <anonymous class>.Utils.unwrapParameters(file:///C:/Users/mehed/AppData/Local/Temp/anonymous2841595966541034444webdriver-profile/extensions/[email protected]/components/driver-component.js:9272)
at <anonymous class>.injectAndExecuteScript(file:///C:/Users/mehed/AppData/Local/Temp/anonymous2841595966541034444webdriver-profile/extensions/[email protected]/components/driver-component.js:10665)
at <anonymous class>.FirefoxDriver.prototype.executeScript(file:///C:/Users/mehed/AppData/Local/Temp/anonymous2841595966541034444webdriver-profile/extensions/[email protected]/components/driver-component.js:10729)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/mehed/AppData/Local/Temp/anonymous2841595966541034444webdriver-profile/extensions/[email protected]/components/command-processor.js:12661)
at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/mehed/AppData/Local/Temp/anonymous2841595966541034444webdriver-profile/extensions/[email protected]/components/command-processor.js:12666)
at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/mehed/AppData/Local/Temp/anonymous2841595966541034444webdriver-profile/extensions/[email protected]/components/command-processor.js:12608)
 

1 Answer

0

StaleElementReferenceException usually happens when you are dealing with list of WebElement and before you work with your element your page is refreshed or moved back.

e.g.

List<WebElement> allElements = driver.findElements(By.xpath("/your/xpath/here"));

Now you are doing some operation in loop

for(WebElement element : allElements)

{

//some operation which makes page refresh or change

}

in above case during second iteration or next iteration, you will face StaleElementReferenceException.

To avoid that you should do as below.

for(WebElement element: allElements){

//some operation which makes page refresh or change

allElements = driver.findElements(By.xpath("/your/xpath/here"));

}

If above goes into loop you can even try below.

for(int i=0;i<allElements.size() ; i++){

element = allElements(i);

//some operation which makes page refresh or change

allElements = driver.findElements(By.xpath("/your/xpath/here"));

}

Hope that helps.

answered Oct 24, 2016 by stbadmin (4,970 points)
...