DeepakVerma
DeepakVerma

Reputation: 421

Selenium Getting Stale exception when navigating to next page using loop

After listing all countries and language, I want to navigate to next page. Below are the code that I am using for the same. With this code I get the first country data and after this i am getting Stale exception.

    int indexGroupRow = 0;  
    for (WebElement rowElement:listRows) 
    {
      if (rowElement.getAttribute("class").contains("dxgvGroupRow") 
          && rowElement.getAttribute("id").contains("Content_gvCountries_DXGroupRowExp"+indexGroupRow))
      {
           String t = rowElement.getText();
           System.out.println("Country: "+t);
      }
      if (rowElement.getAttribute("class").contains("dxgvDataRow") 
          && rowElement.getAttribute("id").contains("Content_gvCountries_DXDataRow"+indexGroupRow))
      {
           String t1 = rowElement.getText();
           System.out.println("Available Language: " + t1);
           List<WebElement> linkElement = rowElement.findElements(By.xpath("td[position()>1]"));
           for (WebElement er: linkElement)
           {
               er.findElement(By.xpath("//*[@id='Content_gvCountries_cell" 
                   + indexGroupRow 
                   + "_2_lnkSelectCountry_" 
                   + indexGroupRow + "']")).click();
           }
      }
      indexGroupRow++;
   }

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=52.0.2743.116) (Driver info: chromedriver=2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 26 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09' System info: host: 'WINJITLAPTOP96', ip: '192.168.224.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67' Session ID: adf74ce205fd64d8b3d5c5064781985b Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\deepakv\AppData\Local\Temp\scoped_dir11036_1170}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=52.0.2743.116, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, hasTouchScreen=false, applicationCacheEnabled=false, takesScreenshot=true}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 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.RemoteWebElement.execute(RemoteWebElement.java:327) at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:179) at LoginTest.testLogin(LoginTest.java:61) Disconnected from the target VM, address: '127.0.0.1:23563', transport: 'socket'

Upvotes: 0

Views: 413

Answers (1)

Vyacheslav Tsivina
Vyacheslav Tsivina

Reputation: 307

for (WebElement er: linkElement)
       {
           er.findElement(By.xpath("//*[@id='Content_gvCountries_cell" + indexGroupRow + "_2_lnkSelectCountry_" + indexGroupRow + "']")).click();
       }

After first click you lose another links, so driver can't click on second element from linkElement list(because it belongs to previous page). You need to find link to next page after each click

Upvotes: 1

Related Questions