2015-12-22 56 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

閱讀關於頁面對象設計模式,測試應該像是一個故事需要非常容易閱讀您需要在閱讀後投資於基礎設施我會很樂意爲您提供幫助 –

相關問題