How to switch control from child window to parent window in selenium webdriver?

  • In the parent window, I pass the control to the child window
  • I perform actions in a child window
  • After execution, another window will open from the child window (Child of the 1st child window).
  • I need to close both child windows and return to the Parent window.

    I cannot switch the control from child to parent. I tried the code below

     String winHandleBefore = _driver.getWindowHandle();
    for(String winHandle : _driver.getWindowHandles()){
        _driver.switchTo().window(winHandle);
    }
    
    String winHandleAfter = _driver.getWindowHandle();
    

    / perform actions in the child window /

    driver.close();
    _driver.switchTo().window(winHandleBefore);
    
+6
source share
8 answers

Use this code:

 // Get Parent window handle
 String winHandleBefore = _driver.getWindowHandle();
 for (String winHandle : _driver.getWindowHandles()) {
   // Switch to child window
   driver.switchTo().window(winHandle);
 }

// Do some operation on child window and get child window handle.
String winHandleAfter = driver.getWindowHandle();

//switch to child window of 1st child window.
for(String winChildHandle : _driver.getWindowHandles()) {
  // Switch to child window of the 1st child window.
  if(!winChildHandle.equals(winHandleBefore) 
  && !winChildHandle.equals(winHandleAfter)) {
    driver.switchTo().window(winChildHandle);
   }
 }

// Do some operation on child window of 1st child window.
// to close the child window of 1st child window.
driver.close();

// to close the child window.
driver.close();

// to switch to parent window.
driver.switchto.window(winHandleBefore);
+2
source

Try it.

  • Close second child window
  • Then there are two windows of the main window and the 1st child window
  • Try getWindowhandles (); it should return 2 window handles.
  • .
  • .

driver.switchTo() defaultContent();.

http://santoshsarmajv.blogspot.in/2012/04/how-to-switch-control-to-pop-up-window.html

0

webdriver

String parent = driver.getWindowHandle ();

Set pops = driver.getWindowHandles (); {

Iterator it = pops.iterator ();

while (it.hasNext ()) {

    String popupHandle=it.next().toString();
    if(!popupHandle.contains(parent))
    {
    driver.switchTo().window(popupHandle);
    System.out.println("Popu Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
    driver.close();
0
source

Try this before calling

driver.switchTo().window(parentwindow);

you should call

driver.switchto.defaultcontent .
0
source
            int i = WebDriver.WindowHandles.Count;
            if (i > 1)
            {
                //switch to child window
                WebDriver.SwitchTo().Window(WebDriver.WindowHandles[1]);
                //switch to parent window
                WebDriver.SwitchTo().Window(WebDriver.WindowHandles[0]);
            }
0
source

Try switching to window handles and returning to the parent window

  String winHandleBefore = driver.getWindowHandle();
  for(String winHandle : driver.getWindowHandles())
  {
  driver.switchTo().window(winHandle);
  }
    for (String handle1 : driver.getWindowHandles())
 { 
  driver.switchTo().window(handle1);
 }

// your code is here

 driver.switchTo().window(winHandleBefore); //switch to parent window
0
source
String Parent_Window = driver.getWindowHandle();    

 for (String Child_Window : driver.getWindowHandles())  
 {  
 driver.switchTo().window(Child_Window);  
 //Perform operation on child window 
 driver.close();
 } 

 //Switching back to Parent Window  
 driver.switchTo().window(Parent_Window);  
0
source

try the following code .. it worked for me ...

public static void main(String[] args) 
{
    System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("https://www.naukri.com/");

    //Get the address of main window
    String p = driver.getWindowHandle();

    //get address of all windows
    Set<String> allWH = driver.getWindowHandles();

    //get address of all windows and store it in ArrayList
    ArrayList<String> allWHCopy = new ArrayList<String>(allWH);

    //close the 2nd window
    for(int i=0;i<allWHCopy.size();i++)
    {
        if(i==2)
        {
            driver.switchTo().window(allWHCopy.get(i));
            driver.close();
        }
    }

    //To switch to main window
    driver.switchTo().window(p);

    //perform action in main window
}
0
source

All Articles