zoukankan      html  css  js  c++  java
  • Selenium 切换句柄

    最近用了网络上别人的一段切换窗口的code每次成功了,不错,学习

    // 根据Title切换新窗口
      public boolean switchToWindow_Title(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 find!!!"
              + e.fillInStackTrace());
          flag = false;
        }
        return flag;
      }
    // 根据URL切换新窗口
      public boolean switchToWindow_Url(WebDriver driver, String windowUrl) {
        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.getCurrentUrl().contains(windowUrl)) {
                flag = true;
                System.out.println("Switch to Window: " + windowUrl
                    + "  successfully~~~!");
                break;
              } else
                continue;
            }
          }
        } catch (NoSuchWindowException e) {
          System.out.println("Window: " + windowUrl + " cound not find!!!"
              + e.fillInStackTrace());
          flag = false;
        }
        return flag;
      }
  • 相关阅读:
    英语中的一个月几天的表示法
    深圳梧桐山游记
    linux中创建文件和文件夹
    linux中~和/的区别
    linux中的--和-的区别
    linux中vi和vim的区别
    基本数据类型大小和范围
    洛谷 [AHOI2001]质数和分解
    codevs 1115 开心的金明--01背包
    codevs 1080 线段树练习--用树状数组做的
  • 原文地址:https://www.cnblogs.com/zoeya/p/5082806.html
Copyright © 2011-2022 走看看