• selenium测试(Java)--多窗口切换(十三)


    如果遇到点击按钮或链接后出现新窗口的情况,就需要使用窗口切换的方法。

    本例中就是先打开百度搜索界面,然后利用js打开一个百度新闻界面,然后通过selenium提供的窗口切换方法切换窗口

    package com.test.windows;
    
    import java.util.Iterator;
    import java.util.Set;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class WindowsSwitch {
    
        public static void main(String[] args) {
    
            WebDriver driver = new FirefoxDriver();
            driver.get("http://www.baidu.com");
            driver.manage().window().maximize();
    
            String searchHandle = driver.getWindowHandle();
            System.out.println("baidu search handle : " + searchHandle);
    
            // 获取百度新闻的连接,然后利用js打开一个新的窗口
            waitTime(3000);
            String href = driver.findElement(By.cssSelector("[name=tj_trnews]")).getAttribute("href");
            System.out.println("the link of news is : " + href);
            JavascriptExecutor executor = (JavascriptExecutor) driver;
            executor.executeScript("window.open('" + href + "')");
    
            waitTime(3000);
            Set<String> handles = driver.getWindowHandles();
            Iterator iterator = handles.iterator();
            Iterator iterator2 = handles.iterator();
    
            // 进入百度新闻窗口,并获取title验证
            waitTime(3000);
            while (iterator.hasNext()) {
                String h = (String) iterator.next();
                if (h != searchHandle) {
                    driver.switchTo().window(h);
                    if (driver.getTitle().contains("百度新闻")) {
                        System.out.println("switch to news page successfully");
                        break;
                    } else {
                        continue;
                    }
                }
    
            }
    
            // 返回百度首页
            waitTime(3000);
            /*
             * 可以直接使用searchHandle来切换,也可以利用遍历来切换
             * driver.switchTo().window(searchHandle);
             * if(driver.getTitle().contains("百度一下")){
             * System.out.println("switch to search page successfully");
             * driver.findElement(By.cssSelector("#kw")).sendKeys(
             * "switch successfully"); }
             */
            while (iterator2.hasNext()) {
                String h = (String) iterator2.next();
                if (searchHandle.equals(h)) {
                    driver.switchTo().window(h);
                    if (driver.getTitle().contains("百度一下")) {
                        System.out.println("switch to search page successfully");
                        driver.findElement(By.cssSelector("#kw")).sendKeys("switch successfully");
                        break;
                    } else {
                        continue;
                    }
                }
    
            }
    
            waitTime(3000);
            driver.quit();
    
        }
    
        static public void waitTime(int time) {
    
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }

    运行结果:

    baidu search handle : {57536d1e-6817-4127-83d6-6eb138f28e51}
    the link of news is : http://news.baidu.com/
    switch to news page successfully
    switch to search page successfully

  • 相关阅读:
    马赛克算法及iOS代码实现
    iOS制作Static Library(静态库),实现多工程的连编
    iOS由ImageIO.framework实现gif的系统解码
    KVC和KVO实现监听容器类(数组等)的变化
    Dynamicaly Typed(动态定型), Objective-C Runtime Programming
    Mac OSX下修改hosts文件
    MAC配置SVN服务器
    Encoding非常用编码转换
    Block作为参数使用
    UITextField关闭系统自动联想和首字母大写功能
  • 原文地址:https://www.cnblogs.com/xinxin1994/p/7289570.html
Copyright © 2020-2023  润新知