I am trying to test this Website. It has multiple product inside is. I want to click on first product and once the product page is open i want to go back to the previous page and then click on the second product. Once it is open i again want to go back and then do the same for all the products available on the webpage.
I wrote the below code to do so -
driver.manage().timeouts().implicitlyWait(8000, TimeUnit.SECONDS);
List<WebElement> product = driver.findElements(By.xpath(".//*[@class='products-grid products-grid--max-4-col first last odd']/li/a"));
for(int i=0; i<product.size();i++){
try{
driver.manage().timeouts().implicitlyWait(8000, TimeUnit.SECONDS);
product.get(i).click();
System.out.println(i);
System.out.println("Title is : "+driver.getTitle());
System.out.println("Product URL is : "+driver.getCurrentUrl());
driver.navigate().back();
}catch(StaleElementReferenceException e){
e.printStackTrace();
}//catch
}//for
After clicking on the first product/element i am getting this exception -
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=44.0.2403.89)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86)(WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 115 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'
System info: host: 'ShantanuNandan', ip: '10.0.0.4', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, chrome= {userDataDir=C:\Users\SHANTA~1\AppData\Local\Temp\scoped_dir6172_17060}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=44.0.2403.89, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 081ee44a67affb195766d4fc8ce165d3
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:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:269)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:80)
at com.Selenium_Practice.Niraame_Login_Logout_ElementDisplay.ClickAndComeBack(Niraame_Login_Logout_ElementDisplay.java:80)
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:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
I went through the same questions asked on the forum and so far did these thing. First i put Thread.sleep and page refresh to make the element available for the click. Code -
driver.navigate().back();
Thread.sleep(4000);
driver.navigate().refresh();
But again i got the same exception.
Then i put a explicitwait instead of implicit wait but again got the same exception.
Then i put continue; in the try block as well as in the the catch block but got the same exception.
I tried 2 more options but all are of no use. Please tell me where i am doing the mistake. I know why it is happening(it is happening because the DOM is not making the elements available after the first click) but not able to figure out a way by which i can get the desired result.
On chrome i am getting StaleElementReferenceException but for the firefox i am not getting any exception after the first click. The firefox browser freezes after the first click.
