如何在Selenium Webdriver中将控件从子窗口切换到父窗口? [英] How to switch control from child window to parent window in selenium webdriver?

查看:325
本文介绍了如何在Selenium Webdriver中将控件从子窗口切换到父窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我从父窗口将控件传递给子窗口
  • 我正在子窗口中执行操作
  • 执行后,将从子窗口中再打开一个窗口(子 第一个子窗口).
  • 我必须关闭两个子窗口,然后必须返回到 父窗口.

  • 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 more window will open(Child of 1st child window).
  • I have to close both the child windows and have to get back to the Parent window.

我无法将控件从子窗口切换到父窗口.我已经尝试了以下代码

I'm not able to switch the control from child to parent window. I have tried out the below code

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

String winHandleAfter = _driver.getWindowHandle();

/在子窗口中执行操作/

driver.close();
_driver.switchTo().window(winHandleBefore);

推荐答案

使用以下代码:

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

这篇关于如何在Selenium Webdriver中将控件从子窗口切换到父窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆