如何在两个窗口之间的浏览器使用Selenium的Java切换 [英] How to switch between two windows in browser using Selenium java

查看:596
本文介绍了如何在两个窗口之间的浏览器使用Selenium的Java切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与硒自动化工作。在此,当我在当前窗口中点击一个链接,打开一个新窗口。我只是想切换控制到新窗口。但我无法做到这一点。其实新的窗口是一个自动生成一个。也就是说,链接将被动态地生成。帮助我的朋友...

I'm working with Selenium Automation. In this, When i click a link in a current window, a new window opens. I just want to switch the control to the new window. But i can't do this.Actually the new window is an auto-generated one. That is, link will be generated dynamically. Help me friends...

推荐答案

我终于找到了答案,
我用下面的方法切换到新的窗口,

I finally found the answer, I used the below method to switch to the new window,

public String switchwindow(String object, String data){
        try {

        String winHandleBefore = driver.getWindowHandle();

        for(String winHandle : driver.getWindowHandles()){
            driver.switchTo().window(winHandle);
        }
        }catch(Exception e){
        return Constants.KEYWORD_FAIL+ "Unable to Switch Window" + e.getMessage();
        }
        return Constants.KEYWORD_PASS;
        }

要移动到父窗口,我用下面的code,

To move to parent window, i used the following code,

 public String switchwindowback(String object, String data){
            try {
                String winHandleBefore = driver.getWindowHandle();
                driver.close(); 
                //Switch back to original browser (first window)
                driver.switchTo().window(winHandleBefore);
                //continue with original browser (first window)
            }catch(Exception e){
            return Constants.KEYWORD_FAIL+ "Unable to Switch to main window" + e.getMessage();
            }
            return Constants.KEYWORD_PASS;
            }

这篇关于如何在两个窗口之间的浏览器使用Selenium的Java切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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