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

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


                      }

最佳答案

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

我的猜测是,您在 for(WebElement option : options) 上遇到异常,因为 else 部分中的一次点击会将您重定向到新页面或删除来自 dom 的选项。即使您返回上一页,您仍然需要再次查找该元素。

你可以尝试这样的事情

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

关于java - selenium webdriver 中的元素不再附加到 DOM 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34410648/

相关文章:

java - 如何克隆一个你不知道其类型的对象?

java - Chromium 与 SeleneseTestCase

java - 反射(reflection)与 Collection

java - 全局导入同一个类

c# - 使用具有 Windows 身份验证的 Selenium Webdriver(在 Chrome 中)C#/ASP.Net

java - 拖放功能在 selenium Webdriver 中不起作用

python - selenium.common.exceptions.WebDriverException : Message: unknown error: Failed to create a Chrome process error with ChromeDriver Chrome Selenium

python - 在 Selenium Webdriver 中呈现页面后禁用 Javascript

Selenium:如何获取自动开始下载的链接?

java - java中int与整数的内存大小