【问题标题】:How to close child browser window in Selenium WebDriver using Java如何使用 Java 在 Selenium WebDriver 中关闭子浏览器窗口
【发布时间】:2013-05-26 04:48:10
【问题描述】:

切换到新窗口并完成任务后,我想关闭那个新窗口并切换到旧窗口,

所以我在这里写了类似代码:

// Perform the click operation that opens new window

String winHandleBefore = driver.getWindowHandle();

    // Switch to new window opened

    for (String winHandle : driver.getWindowHandles()) {
        driver.switchTo().window(winHandle);
    }

    // Perform the actions on new window


    driver.findElement(By.id("edit-name")).clear();
    WebElement userName = driver.findElement(By.id("edit-name"));
    userName.clear();
              try
    {
        driver.quit();
    }

    catch(Exception e)
    {
        e.printStackTrace();
        System.out.println("not close");
                }

driver.switchTo().window(winHandleBefore);// Again I want to start code this old window

上面我写了代码driver.quit()driver.close()。但我收到错误。谁能帮帮我...?

org.openqa.selenium.remote.SessionNotFoundException: 调用 quit() 后无法使用 FirefoxDriver。

【问题讨论】:

    标签: java selenium selenium-webdriver webdriver


    【解决方案1】:

    我也试过了

    1)driver.close();
    2)driver.quit();

    显然,这些方法没有按预期工作!(不是说它不起作用)我什至尝试过将驱动程序类设为单例,但它并没有帮助我并行运行测试用例。所以它是也不是最佳解决方案。最后,我想出了一个单独的类来运行一个 bat 文件。该 bat 文件包含对所有 chrome 驱动程序进程和它的所有子进程进行分组的命令。并且来自我的 java 类使用运行时执行它。

    运行bat文件的类文件

    public class KillChromeDrivers {
    
        public static void main(String args[]) {
    
    
            try {
    
                Runtime.getRuntime().exec("cmd /c start E:\\Work_Folder\\SelTools\\KillDrivers.bat");
                //Runtime.getRuntime().exec()
            } catch (Exception ex) {
    
    
    
            }
        }
    
    }
    

    您必须放入 [ .bat ] 文件的命令

    taskkill /IM "chromedriver.exe" /T /F
    

    【讨论】:

      【解决方案2】:

      关闭单个浏览器窗口:

      driver.close();
      

      关闭所有(父+子)浏览器窗口并结束整个会话:

      driver.quit();
      

      【讨论】:

        【解决方案3】:
        public class First {
            public static void main(String[] args) {
                System.out.println("Welcome to Selenium");
                WebDriver wd= new FirefoxDriver();
                wd.manage().window().maximize();
                wd.get("http://opensource.demo.orangehrmlive.com/");
                wd.findElement(By.id("txtUsername")).sendKeys("Admin");
                wd.findElement(By.id("txtPassword")).sendKeys("admin");
                wd.findElement(By.id("btnLogin")).submit();
                **wd.quit(); //--> this helps to close the web window automatically** 
                System.out.println("Tested Sucessfully ");
            }
        }
        

        【讨论】:

          【解决方案4】:

          关闭单个子窗口有两种方式:

          方式一:

          driver.close();
          

          方式 2: 使用键盘上的 Ctrl+w 键:

          driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
          

          【讨论】:

          • driver.close();适用于新窗口。 driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");适用于新窗口和新标签。
          【解决方案5】:
          //store instance of main window first using below code
          String winHandleBefore = driver.getWindowHandle(); 
          

          执行打开新窗口的点击操作

          //Switch to new window opened
          for (String winHandle : driver.getWindowHandles()) {
              driver.switchTo().window(winHandle);
          }
          
          // Perform the actions on new window
          driver.close(); //this will close new opened window
          
          //switch back to main window using this code
          driver.switchTo().window(winHandleBefore);
          
          // perform operation then close and quit
          driver.close();
          driver.quit();
          

          【讨论】:

            【解决方案6】:

            在某些情况下,从 getWindowHandle() 或 getWindowHandles() 获得有效的窗口句柄后,窗口会自行关闭。

            甚至有可能在 getWindowHandles() 运行时窗口会自行关闭,除非您创建一些临界区类型的代码(即在运行测试代码时冻结浏览器,直到所有窗口管理操作完成)

            检查当前驱动程序有效性的一种更快的方法是检查 sessionId,它被 driver.close() 或窗口关闭本身设为 null。

            需要将WebDriver强制转换为远程驱动接口(RemoteWebDriver)才能获取sessionId,如下:

            if (null == ((RemoteWebDriver)driver).sessionId) {
                // current window is closed, switch to another or quit
            } else {
                // current window is open, send commands or close
            }
            

            还要注意关闭最后一个窗口等价于quit()。

            【讨论】:

              【解决方案7】:

              您用于将控件切换到弹出窗口的逻辑是错误的

               for (String winHandle : driver.getWindowHandles()) {
                      driver.switchTo().window(winHandle);
                  }
              

              上述逻辑如何将控件切换到新窗口?


              使用以下逻辑将控件切换到新窗口

              // get all the window handles before the popup window appears
               Set beforePopup = driver.getWindowHandles();
              
              // click the link which creates the popup window
              driver.findElement(by).click();
              
              // get all the window handles after the popup window appears
              Set afterPopup = driver.getWindowHandles();
              
              // remove all the handles from before the popup window appears
              afterPopup.removeAll(beforePopup);
              
              // there should be only one window handle left
              if(afterPopup.size() == 1) {
                        driver.switchTo().window((String)afterPopup.toArray()[0]);
               }
              

              // Perform the actions on new window

                **`//Close the new window`** 
                  driver.close();
              

              //perform remain operations in main window

                 //close entire webDriver session
                  driver.quit();
              

              【讨论】:

                最近更新 更多