gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:32:58 33 4
gpt4 key购买 nike

列出所有国家/地区和语言后,我想导航到下一页。下面是我使用的代码。通过此代码,我获得了第一个国家/地区数据,此后我收到了 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com