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

后端 未结 8 1398
一个人的身影
一个人的身影 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: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);
    

提交回复
热议问题