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

后端 未结 8 1322
一个人的身影
一个人的身影 2020-12-03 17:38
  • From Parent window I\'m passing the control to child window
  • I\'m performing actions in the child window
  • After performing, from a child window one mor
相关标签:
8条回答
  • 2020-12-03 18:15
    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 讨论(0)
  • 2020-12-03 18:18

    Try like this.

    1. Close 2nd child window
    2. Then there are two windows main window and 1st child window
    3. Try to getWindowhandles(); it should return 2 window handles.
    4. From that two window handles extract main window handle.
    5. Switch back to main window using above window handle.

    OR

    driver.switchTo().defaultContent();

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

    0 讨论(0)
  • 2020-12-03 18:25

    try the below 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 讨论(0)
  • 2020-12-03 18:28

    to close multiple popups in webdriver and switch to parent window

    String parent = driver.getWindowHandle();
    
      Set<String> pops=driver.getWindowHandles();
        {
    
    Iterator<String> 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 讨论(0)
  • 2020-12-03 18:29

    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);
    
    0 讨论(0)
  • 2020-12-03 18:35

    Try this before calling

    driver.switchTo().window(parentwindow);
    

    you should call

    driver.switchto.defaultcontent .
    
    0 讨论(0)
提交回复
热议问题