Selenium 新窗口处理方法

有时点击一个链接,跳到一个新的窗口,这时WebDriver需切换到新窗口,才能对新窗口的元素进行操作

这里封装一个switchToWindow方法:

public boolean switchToWindow(WebDriver driver,String windowTitle){  

        boolean flag = false;  

        try {  

            String currentHandle = driver.getWindowHandle();  

            Set<String> handles = driver.getWindowHandles();  

            for (String s : handles) {  

                if (s.equals(currentHandle))  

                    continue;  

                else {  

                    driver.switchTo().window(s);  

                    if (driver.getTitle().contains(windowTitle)) {  

                        flag = true;  

                        System.out.println("Switch to window: "  

                                + windowTitle + " successfully!");  

                        break;  

                    } else  

                        continue;  

                }  

            }  

        } catch (NoSuchWindowException e) {  

            System.out.println("Window: " + windowTitle  

                    + " cound not found!");  

            flag = false;  

        }  

        return flag;  

    }  



}

上一篇:C语言常用函数笔记


下一篇:java模拟数据库缓存