简体   繁体   English

使用Java关闭Selenium Automation中的当前窗口后如何切换到现有窗口

[英]How to switch to existing window after closing the current window in Selenium Automation using java

Actually, i'm trying to switch the control from current window to existing window after closing the current one in Selenium Automation using java. 实际上,在使用Java关闭Selenium Automation中的当前窗口后,我试图将控件从当前窗口切换到现有窗口。 Is there any way to do that. 有没有办法做到这一点。 I'm able to control the newly opened window, doing some process & closing this one. 我能够控制新打开的窗口,进行一些处理并关闭此窗口。 Later i just need to move to already existing browser window. 稍后,我只需要移至现有的浏览器窗口即可。

This is something that I use , this checks for all the open windows and then switchess control to the next window with an option to close out the older window. 这是我使用的东西,它将检查所有打开的窗口,然后将控件切换到下一个窗口,并带有关闭旧窗口的选项。

protected final void switchWindows(boolean closeOldWindow) {
    final WebDriver driver = checkNotNull(getDriver(), "missing WebDriver");

    final String currentWindow = driver.getWindowHandle();
    checkNotNull(currentWindow);

    // switch to first window that is not equal to the current window
    String newWindow = null;
    for (final String handle : driver.getWindowHandles()) {
        if (!currentWindow.equals(handle)) {
            newWindow = handle;
            break;
        }
    }

    // if there's another window found...
    if (newWindow != null) {
        if (closeOldWindow) {
            // close the current window
            driver.close();
        }
        // ...switch to the new window
        driver.switchTo().window(newWindow);
    }
}

I just know selenium js webdriver have a api like driver.switchTo().window(windowName) could help us move to another window. 我只知道硒JS的webdriver有一个像API driver.switchTo().window(windowName)可以帮助我们移动到另一个窗口。 I am not so familiar with Java API but they are all from almost same invoking way. 我不是很熟悉Java API,但他们都几乎相同的调用方式。 hope this would help you. 希望对您有帮助。

you can use this- 您可以使用此-

        private void handleMultipleWindows(String windowTitle) {
        Set<String> windows = driver.getWindowHandles();

        for (String window : windows) {
            driver.switchTo().window(window);
            if (driver.getTitle().contains(windowTitle)) {
                return; 
            }
        }
    }

I have an utility method to switch to the required window as shown below 我有一个实用方法可以切换到所需的窗口,如下所示

public class Utility 
{
    public static WebDriver getHandleToWindow(String title){

        //parentWindowHandle = WebDriverInitialize.getDriver().getWindowHandle(); // save the current window handle.
        WebDriver popup = null;
        Set<String> windowIterator = WebDriverInitialize.getDriver().getWindowHandles();
        System.err.println("No of windows :  " + windowIterator.size());
        for (String s : windowIterator) {
          String windowHandle = s; 
          popup = WebDriverInitialize.getDriver().switchTo().window(windowHandle);
          System.out.println("Window Title : " + popup.getTitle());
          System.out.println("Window Url : " + popup.getCurrentUrl());
          if (popup.getTitle().equals(title) ){
              System.out.println("Selected Window Title : " + popup.getTitle());
              return popup;
          }

        }
                System.out.println("Window Title :" + popup.getTitle());
                System.out.println();
            return popup;
        }
}

It will take you to desired window once title of the window is passed as parameter. 将窗口的标题作为参数传递后,它将带您到所需的窗口。 In your case you can do. 根据您的情况,您可以做。

Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");

and then again switch to parent window using the same method 然后使用相同的方法再次切换到父窗口

Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");

This method works effectively when dealing with multiple windows 该方法在处理多个窗口时有效

Once you closed the current window, you will be seeing the parent window. 关闭当前窗口后,您将看到父窗口。 To switch back to your parent/previous window you can use the following commands, 要切换回父窗口/上一个窗口,可以使用以下命令,

  • driver.getUIWindowLocator().switchToFirstWindow(); driver.getUIWindowLocator()。switchToFirstWindow();

  • driver.switchTo().defaultContent(); driver.switchTo()。defaultContent();

commands. 命令。 I think this will be helpful for you. 我认为这将对您有所帮助。

Please do following code for switching back to base window or original window. 请执行以下代码以切换回基本窗口或原始窗口。

String vBaseWindowHandle = driver.getWindowHandle();
    Set<String> windows = driver.getWindowHandles();

    for(String temp : windows)
    {
        driver.switchTo().window(temp);
    }

    // Do code for new window

    driver.close();     
    driver.switchTo().window(vBaseWindowHandle);

For more information about how to handle Windows and popup in selenium please go to below URL. 有关如何处理Windows和硒中弹出窗口的更多信息,请转到下面的URL。 https://trickyautomationworld.blogspot.in/2018/03/how-to-handle-windows-in-selenium.html https://trickyautomationworld.blogspot.in/2018/03/how-to-handle-popup-in-selenium.html https://trickyautomationworld.blogspot.in/2018/03/how-to-handle-windows-in-selenium.html https://trickyautomationworld.blogspot.in/2018/03/how-to-handle-popup-in -selenium.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM