1
votes

I'm getting the above mentioned for the below code:

  1. Opens a page
  2. Check if the new month is available
  3. Downloads new month ex: Oct
  4. Then comes out of loop and should download Sep

But after coming out of loop it is throwing above error message.

Here is the error log:

Download new month Oct month data downloaded in csv format Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM Command duration or timeout: 21 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12' System info: host: 'Treselle', ip: '192.168.0.123', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_60' Session ID: 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: Element is no longer attached to the DOM For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:59:12' System info: host: 'Treselle', ip: '192.168.0.123', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_60' Driver info: driver.version: unknown at .fxdriver.cache.getElementAt(resource://fxdriver/modules/web-element-cache.js:9354) at .Utils.getElementAt(file:///C:/Users/Ashik/AppData/Local/Temp/anonymous7980863778031317345webdriver-profile/extensions/[email protected]/components/command-processor.js:8978) at .WebElement.getElementText(file:///C:/Users/Ashik/AppData/Local/Temp/anonymous7980863778031317345webdriver-profile/extensions/[email protected]/components/command-processor.js:11965) at .DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/Ashik/AppData/Local/Temp/anonymous7980863778031317345webdriver-profile/extensions/[email protected]/components/command-processor.js:12534) at .DelayedCommand.prototype.executeInternal_(file:///C:/Users/Ashik/AppData/Local/Temp/anonymous7980863778031317345webdriver-profile/extensions/[email protected]/components/command-processor.js:12539) at .DelayedCommand.prototype.execute/<(file:///C:/Users/Ashik/AppData/Local/Temp/anonymous7980863778031317345webdriver-profile/extensions/[email protected]/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");
        }
1
may you attach whole error log?Mesut GUNES
Yuck... hard code Waits... The error is telling you that your are trying to access an element that is no longer attached to the DOM, the page either updated the DOM dynamically or it was reloadedJuan Mendes

1 Answers

0
votes

init again after last call in the loop.

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

or you can do it in on row

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

in your code it should look like:

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"));
        }
}