How to close child browser window in Selenium WebDriver using Java

后端 未结 7 1620
傲寒
傲寒 2020-12-14 16:07

After I switch to a new window and complete the task, I want to close that new window and switch to the old window,

so here i written like code:

// P         


        
相关标签:
7条回答
  • 2020-12-14 16:46
    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 ");
        }
    }
    
    0 讨论(0)
  • 2020-12-14 16:50
    //store instance of main window first using below code
    String winHandleBefore = driver.getWindowHandle(); 
    

    Perform the click operation that opens new window

    //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();
    
    0 讨论(0)
  • 2020-12-14 16:56

    The logic you've used for switching the control to popup is wrong

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

    How the above logic will swtich the control to new window ?


    Use below logic to switch the control to new window

    // 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();
    
    0 讨论(0)
  • 2020-12-14 16:57

    To close a single browser window:

    driver.close();
    

    To close all (parent+child) browser windows and end the whole session:

    driver.quit();
    
    0 讨论(0)
  • There are 2 ways to close the single child window:

    Way 1:

    driver.close();
    

    Way 2: By using Ctrl+w keys from keyboard:

    driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
    
    0 讨论(0)
  • 2020-12-14 17:02

    There are some instances where a window will close itself after a valid window handle has been obtained from getWindowHandle() or getWindowHandles().

    There is even a possibility that a window will close itself while getWindowHandles() is running, unless you create some critical section type code (ie freeze the browser while running the test code, until all window management operations are complete)

    A quicker way to check the validity of the current driver is to check the sessionId, which is made null by driver.close() or by the window closing itself.

    The WebDriver needs to be cast to the remote driver interface (RemoteWebDriver) in order to obtain the sessionId, as follows:

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

    Also note that closing the last window is equivalent to quit().

    0 讨论(0)
提交回复
热议问题