java - java元素不再附加到DOM命令的持续时间或者超时: 21毫秒,当它从循环中出来时

  显示原文与译文双语对照的内容
99 3

我得到了 below 代码所提到的above:

  • 打开页面
  • 检查新月份是否可用
  • 下载新月份:Oct
  • 然后从循环开始下载

但是在循环之后,它抛出 above 错误消息。

以下是错误日志:

下载"主要" org.openqa.selenium.StaleElementReferenceException 中以csv格式下载的月份月数据:元素不再附加到DOM命令的持续时间或者超时:有关这里错误的文档的21毫秒,请访问:http://seleniumhq.org/exceptions/stale_element_reference.html 插件构建信息:版本:'2.48.2',修订版:'41bccdd',时间:'2015-10-09 19:59:12系统信息:主机:'treselle',ip:'192.168.0.123',os.name:'Windows 7'。os.arch:'amd64,os.version:"6.1'。java.version:'1.7.0_60"会话标识:ecbdefce-d119-43cc-b239-124d9961bf7e Driver info : org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=43.0.1} ] 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:647) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:326) at org.openqa.selenium.remote.RemoteWebElement.getText(RemoteWebElement.java:178) at stack3.main(stack3.java:43) Caused by :org.openqa.selenium.StaleElementReferenceException: 元素不再附加到DOM以获取有关这里错误的文档,请访问:http://seleniumhq.org/exceptions/stale_element_reference.html 插件构建信息:版本:'2.48.2',修订版:'41bccdd',时间:'2015-10-09 19:59:12系统信息:主机:'treselle',ip:'192.168.0.123',os.name:'Windows 7'。os.arch:'amd64,os.version:"6.1'。java.version:'1.7.0_60的驱动程序信息:driver.version: 在. fxdriver.cache.getElementAt(resource://fxdriver/modules/web-element-cache.js:9354) 。prototype处的. WebElement.getElementText(file:///C:/Users/Ashik/AppData/Local/Temp/anonymous7980863778031317345webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:11965) 处,在处未知,位于. DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/Ashik/AppData/Local/Temp/anonymous7980863778031317345webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12534) 处。prototype/<(file:///C:/Users/Ashik/AppData/Local/Temp/anonymous7980863778031317345webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)

driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
driver.manage().window().maximize();
WebElement select = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue"));
List <WebElement> options = select.findElements(By.tagName("option"));
for (WebElement option: options) {
 if (option.getText().equals("Sep 2015 (Unconventional wells)")) {
 System.out.println("old month");
 break;
 } else {
 if (option.getText().contains("Oct")) {
 System.out.println("Download new month");
 WebElement identifier = driver.findElement(By.xpath(".//*
[@id='ReportViewerControl_ctl04_ctl03_ddValue']"));
 Select select1 = new Select(identifier);
 select1.selectByVisibleText("Oct 2015 (Unconventional wells)");
 Wait(20000);
 driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl00']")).click();
 Wait(20000);
 driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Button']/tbody/tr/td/*")).click();
 Wait(20000);
 driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Menu']/div[2]/a")).click();
 Wait(10000);
 System.out.println("Oct month data downloaded in csv format");
 }
时间:原作者:0个回答

150 3

在循环中最后一次调用后再次初始化。

select = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue"));
options = select.findElements(By.tagName("option"));

或者你可以在网上做

options = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue")).findElements(By.tagName("option"));

在你的代码中,它应该类似于:

driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
driver.manage().window().maximize();
WebElement select = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue"));
List <WebElement> options = select.findElements(By.tagName("option"));
for (WebElement option: options) {
 if (option.getText().equals("Sep 2015 (Unconventional wells)")) {
 System.out.println("old month");
 break;
 } else {
 if (option.getText().contains("Oct")) {
 System.out.println("Download new month");
 WebElement identifier = driver.findElement(By.xpath(".//*
[@id='ReportViewerControl_ctl04_ctl03_ddValue']"));
 Select select1 = new Select(identifier);
 select1.selectByVisibleText("Oct 2015 (Unconventional wells)");
 Wait(20000);
 driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl00']")).click();
 Wait(20000);
 driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Button']/tbody/tr/td/*")).click();
 Wait(20000);
 driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Menu']/div[2]/a")).click();
 Wait(10000);
 System.out.println("Oct month data downloaded in csv format");
 options = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue")).findElements(By.tagName("option"));
 }
}
原作者:
...