如何在selenium webdriver中将控制从子窗口切换到父窗口?

时间:2013-01-31 07:07:53

标签: selenium selenium-webdriver

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

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

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

    / 在子窗口中执行操作 /

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

8 个答案:

答案 0 :(得分:2)

使用此代码:

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

答案 1 :(得分:0)

试试这样。

  1. 关闭第二个子窗口
  2. 然后有两个窗口主窗口和第一个子窗口
  3. 尝试getWindowhandles();它应该返回2个窗口句柄。
  4. 从那两个窗口句柄中提取主窗口句柄。
  5. 使用上面的窗口句柄切换回主窗口。
  6. OR

    driver.switchTo()defaultContent();

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

答案 2 :(得分:0)

关闭webdriver中的多个弹出窗口并切换到父窗口

String parent = driver.getWindowHandle();

设置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();

答案 3 :(得分:0)

试试这个 在致电

之前
driver.switchTo().window(parentwindow);

你应该致电

driver.switchto.defaultcontent .

答案 4 :(得分:0)

            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]);
            }

答案 5 :(得分:0)

尝试此操作切换到差异窗口句柄并返回父窗口

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

//你的代码在这里

 driver.switchTo().window(winHandleBefore); //switch to parent window

答案 6 :(得分:0)

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

答案 7 :(得分:-1)

尝试下面的代码..它对我有用...

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
}