2015-12-22 42 views
1

首先它正在执行else部分,然后在打印println后,它会发出此错误。不知道在退出循环后它是否发生错误。请帮忙。元素不再附加到硒webdriver的DOM错误

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


         } 

回答

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()) 
+0

我可以知道如何克服呢? – user2762008

+0

我编辑了我的答案。 – Guy

+0

我认为一个新的页面正在被打开,当你正在下载文件,这是从'DOM'删除'选项' – Paras

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; 
      } 
     } 
+0

我用css,id和name都是失败:(但是没有改变点击这个呢。请帮忙 – user2762008

+0

我可以知道上面的代码放在哪里吗? – user2762008

+0

阅读关于页面对象设计模式,测试应该像是一个故事需要非常容易阅读您需要在阅读后投资于基础设施我会很乐意为您提供帮助 –

相关问题