zoukankan      html  css  js  c++  java
  • Java+Selenium 如何实现关闭当前窗口并返回上一个窗口-driver.close()

    Java+Selenium 如何实现关闭当前窗口并返回上一个窗口

    使用selenium中的driver.close()函数

    如果直接使用driver.close()函数,程序会报错,找不到页面tagat,
    可以使用以下方法实现

    public void closeWindow() {
    
            try {
                String winHandleBefore = driver.getWindowHandle();//关闭当前窗口前,获取当前窗口句柄
                Set<String> winHandles = driver.getWindowHandles();//使用set集合获取所有窗口句柄
    
                driver.close();//关闭窗口
    
                Iterator<String> it = winHandles.iterator();//创建迭代器,迭代winHandles里的句柄
                while (it.hasNext()) {//用it.hasNext()判断时候有下一个窗口,如果有就切换到下一个窗口
                    String win = it.next();//获取集合中的元素
                    if (!win.equals(winHandleBefore)) { //如果此窗口不是关闭前的窗口
                        driver.switchTo().window(win);//切换到新窗口
                        logger.info("Switch Window From " + winHandleBefore + " to " + win);
                        break;
                    }
                }
    
            } catch (Exception e) {
                e.printStackTrace();
    
            }
    
        }
    
  • 相关阅读:
    windows常规
    oracle常规操作
    idea使用
    java-maven
    java-rabbimq
    centos7 rabbitMq 安装教程
    Leetcode 332.重新安排行程
    Leetcode 334.递增的三元子序列
    Leetcode 331.验证二叉树的前序序列化
    Leetcode 330.按要求补齐数组
  • 原文地址:https://www.cnblogs.com/mingyue5826/p/12466988.html
Copyright © 2011-2022 走看看