元素不再附加到selenium webdriver中的DOM错误

时间:2015-12-22 07:34:50

标签: java selenium selenium-webdriver

首先它正在执行else部分,然后在打印println之后,它正在发出此错误。退出循环后不知道是否出错。请帮忙。

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

但是在退出循环之后,它会抛出错误信息。

 driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
            //maximizing the window
              driver.manage().window().maximize();

               //WebElement select = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue"));

              // Select select=new Select(select);
               List<WebElement> options = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue")).findElements(By.tagName("option"));
               //List <WebElement> Element1 = new ArrayList<WebElement>() ;

               for(WebElement option : options){

                  if(option.getText().equals("Sep 2015 (Unconventional wells)")) {

                      driver.get("http://www.depreportingservices.state.pa.us/ReportServer/Pages/ReportViewer.aspx?%2fOil_Gas%2fOil_Gas_Well_Historical_Production_Report");
                         driver.wait(20000);
                             //options.wait(10000);
                          driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue")); 

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

                         select1.selectByVisibleText("Oct 2015 (Unconventional wells)");

                          Wait(20000);
                          driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl04_ctl00']")).click();
                          Wait(70000);
                          //Click on File save button
                          driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Button']")).click();
                          //wait time to load the options
                          Wait(20000);
                          driver.findElement(By.xpath(".//*[@id='ReportViewerControl_ctl05_ctl04_ctl00_Menu']/div[2]/a")).click();
                          //fprofile.setPreference( "browser.download.manager.showWhenStarting", false );
                          //fprofile.setPreference( "pdfjs.disabled", true );
                          Wait(10000);

                         // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                          System.out.println("Oct month data downloaded in csv format");
                          //driver.navigate().back();


                      }

2 个答案:

答案 0 :(得分:1)

该异常意味着您尝试使用的元素不再存在于html中。

我的猜测是,您在for(WebElement option : options)上获得了例外,因为else部分中的一次点击会将您重定向到新页面或从dom中删除options。即使你回到上一页,你仍然需要再次找到该元素。

您可以尝试这样的事情

List<WebElement> options;
int i = 0;
do
{
    options = driver.findElement(By.id("ReportViewerControl_ctl04_ctl03_ddValue")).findElements(By.tagName("option"));
    if(options.get(i).getText().equals("Sep 2015 (Unconventional wells)"))
    {
    }
    else()
    {
    }
} while (i++ < options.size())

答案 1 :(得分:0)

    1. don't use wait 
    2. use css and not xpath 
    3 .read about page object design pattern 
    4 try to use this instead of the click 

after you clean your code you can find the problem easily 


    public static WebElement findElement(WebDriver driver, By selector, long timeOutInSeconds, String timeOutMessage) {
            try {
                WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
                wait.until(ExpectedConditions.presenceOfElementLocated(selector));

                return findElement(driver, selector);
            } catch (TimeoutException e) {
                throw new IllegalStateException(timeOutMessage);
            }
        }

        public static WebElement findElementSafe(WebDriver driver, By selector, long timeOutInSeconds) {
            try {
                return findElement(driver, selector, timeOutInSeconds);
            } catch (TimeoutException e) {
                return null;
            }
        }