• 【java+selenium3】多窗口window切换及句柄handle获取(四)


    一 、页面准备

    1.html

    <html>
        <head>
            <title>主页面 1</title>
        </head>
        <body>
            <div>
                主页面 1 :<input type="text" id="input_1" value=""/><br><br>
                <a href="./2.html" target="_blank">点击这里跳转新页面 2 </a><br><br>
                <a href="./3.html" target="_blank">点击这里跳转新页面 3 </a><br><br>
            </div>
        </body>
    </html>

    2.html

    <html>
        <head>
            <title>新页面 2</title>
        </head>
        <body>
            <div>
                新页面 2 :<input type="text" id="input_2"/>
            </div>
        </body>
    </html>

    3.html

    <html>
        <head>
            <title>新页面 3</title>
        </head>
        <body>
            <div>
                新页面 3 :<input type="text" id="input_3"/>
            </div>
        </body>
    </html>

    二、句柄

    1.获取当前窗口的句柄(String类型)

    String handle = driver.getWindowHandle();

    2.获取所有带开窗口的句柄(set<String>)

    //获取当前打开窗口的所有句柄
    Set<String> handles = driver.getWindowHandles();

    三、项目实战

    package cn.test;
    
    import java.util.ArrayList;
    import java.util.Set;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class Test {
        
        public static void main(String[] args) throws InterruptedException {
            System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
            WebDriver driver =null;
            try {
                driver = new ChromeDriver();
                driver.get("file:///C:/Users/Administrator/Desktop/test/1.html");
                driver.manage().window().maximize();
                driver.findElement(By.id("input_1")).sendKeys("主页面 1 写入数据");
                Thread.sleep(2000);
                //获取主页面1句柄
                String handle1 = driver.getWindowHandle();
                System.out.println("主页面1句柄:"+handle1);
                //点击页面2的超链接跳转后,在页面2文本框输入数据
                driver.findElement(By.partialLinkText("点击这里跳转新页面 2")).click();
                String handle = getLastHandle(driver);
                System.out.println("新页面2句柄:"+handle);
                //获取到页面2的句柄,将driver作用域切到页面2
                driver.switchTo().window(handle);
                driver.findElement(By.id("input_2")).sendKeys("新打开页面 2 输入数据");
                
                //返回主页面1点击打开新页面3 
                //利用页面1的句柄,将driver作用域切回到页面1
                driver.switchTo().window(handle1);
                //点击页面3的超链接跳转后,在页面3文本框输入数据
                driver.findElement(By.partialLinkText("点击这里跳转新页面 3")).click();
                handle = getLastHandle(driver);
                System.out.println("新页面3句柄:"+handle);
                driver.switchTo().window(handle);
                driver.findElement(By.id("input_3")).sendKeys("新打开页面 3 输入数据");
                Thread.sleep(2000);
                //关闭新打开的页面窗口3
                driver.close();
                Thread.sleep(5000);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                driver.quit();
            }
        }
    
        /**
         * @param driver
         * @return 当前打开窗口的最后一个句柄
         */
        public static String getLastHandle(WebDriver driver) {
            //获取当前打开窗口的所有句柄
            Set<String> Allhandles = driver.getWindowHandles();
            ArrayList<String> lst = new ArrayList<String>(Allhandles);
            return lst.get(lst.size()-1);
        }    
    }

    四、学习后总结,不足之处后续补充。。。

  • 相关阅读:
    iOS基础知识----数据解析
    iOS 向下取整、向上取整、四舍五入
    SourceTree推送时,增加额外的远程仓库,不用每次都自定义粘贴复制网络
    Container View 使用小技巧
    CocoaPods 升级
    新版百度云如何加速
    环信透传消息,无法回调
    Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibr
    Mac Sublime Text complie python .py error /bin/bash: shell_session_update: command not found
    ios 消息转发初探
  • 原文地址:https://www.cnblogs.com/xiaozhaoboke/p/11127820.html
Copyright © 2020-2023  润新知