java - 使用循环导航到下一页时,Selenium 出现过时异常

标签 java selenium

列出所有国家/地区和语言后,我想导航到下一页。下面是我使用的代码。通过此代码,我获得了第一个国家/地区数据,此后我收到了 Stale 异常。

    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'

最佳答案

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

第一次单击后,您会丢失另一个链接,因此驱动程序无法单击 linkElement 列表中的第二个元素(因为它属于上一页)。 每次点击后您需要找到下一页的链接

关于java - 使用循环导航到下一页时,Selenium 出现过时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39248047/

相关文章:

python - Selenium 脚本在控制台工作,在 CRON 中不工作 - Geckodriver 错误

java - 一定时间后开卡

c# - 我可以在Java中通过引用传递参数吗?

java - 如何配置 java jackson 不在空 bean 上抛出 NPE 异常?

在 Jenkins 中使用自定义工作区并运行基于 TestNG 的 Selenium 测试的 shell 脚本时出现 java.nio.file.AccessDeniedException 错误

python - 创建一个新文件夹并将其作为 Selenium 中的默认下载

java - 执行错误,从org.apache.hadoop.hive.ql.exec.FunctionTask返回代码1

java - 如何从特定点开始迭代二维数组?

python - NoSuchElementException : Message: Unable to locate element while trying to locate an element using Selenium and Python

javascript - Selenium C# 如何获取 IWEbElement 中的文本