Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close child browser window in Selenium WebDriver using Java

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:

// 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 

Above I written code driver.quit() or driver.close(). But I am getting error. Can anybody help me...?

org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.

like image 596
NandKishor Avatar asked May 30 '13 14:05

NandKishor


People also ask

How do you close a child window?

A child window opens as separate window as a tab. Go back to the parent and click “Close child” and the child window closes. Viola!

How do I stop my child's browser popping up?

We can close the child browser window in Selenium webdriver. The getWindowHandles and getWindowHandle methods can be used to handle child windows. The getWindowHandles method is used to store all the opened window handles in the Set data structure.

How do I close a browser in Selenium?

quit() are two different methods for closing the browser session in Selenium WebDriver . Understanding both of them and knowing when to use which method is important in your test execution. driver. close() – It closes the the browser window on which the focus is set.


2 Answers

To close a single browser window:

driver.close(); 

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

driver.quit(); 
like image 87
Mark Rowlands Avatar answered Sep 25 '22 12:09

Mark Rowlands


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(); 
like image 28
Santoshsarma Avatar answered Sep 22 '22 12:09

Santoshsarma