【问题标题】:Unclear about driver.getWindowHandles() & driver.getWindowHandle()不清楚 driver.getWindowHandles() 和 driver.getWindowHandle()
【发布时间】:2021-05-12 00:20:10
【问题描述】:

大家好,我正在学习 Selenium,我不太清楚上述两个函数是如何工作的: 问题陈述:

我有一个练习作业说:转到http://the-internet.herokuapp.com/

点击一个链接> 多个窗口打开一个窗口> 点击>> 点击这里打开另一个窗口>> 从这个窗口抓取文本并打印它然后返回到这个http://the-internet.herokuapp.com/windows 并打印文本。

流程:http://the-internet.herokuapp.com/>>>http://the-internet.herokuapp.com/windows>>>http://the-internet.herokuapp.com/windows/new

Que1) 如果我使用 driver.getWindowHandle() 并为每个窗口打印它,它的值保持不变,所以这个方法总是返回父窗口或者它的工作方式不同。

Ques2) 当我使用 driver.getWindowHandles() 时,它返回集合中的 2 个值。 driver.getWindowHandles() 是否也返回父窗口。 (我不确定是否应该有 2 个或 3 个值,因为我有 3 个 URL,我认为该集合应该有 3 个)

问题3) 有人可以分享使用多个子窗口 id 的最有效方法吗:

用迭代器方法设置 人们还将 Set 转换为 Arraylist,然后使用 get 方法。 [这是一个更好的方法]

代码:

driver.get("http://the-internet.herokuapp.com/");
        String p1=driver.getWindowHandle();
        System.out.println(p1);
        text1=driver.findElement(By.xpath("//a[@href='/windows']"));
        text1.click();
        WebElement 
        text2=driver.findElement(By.xpath("//a[@href='/windows/new']"));
        text2.click();
        Set<String> child=driver.getWindowHandles();
        System.out.println(child.size());
        ArrayList<String> children=new ArrayList<String>(child);
        System.out.println(children);
        driver.switchTo().window(children.get(1));
        System.out.println(driver.findElement(By.xpath("//div[@class='example']/h3")).getText());
        driver.close();
        driver.switchTo().window(children.get(0));
        System.out.println(driver.findElement(By.xpath("//div[@class='example']/h3")).getText());
        driver.switchTo().window("");
        System.out.println(driver.getCurrentUrl());
        driver.close();

【问题讨论】:

  • 由于有WebElement 的行,该代码似乎无法编译——我可以假设您在运行此代码时已修复该问题吗?

标签: java selenium selenium-webdriver webdriver


【解决方案1】:
            driver.get("http://the-internet.herokuapp.com/");
            String p1=driver.getWindowHandle(); //Gets the newly opened and the only window handle

            System.out.println("This is parent window handle " + p1);
            text1=driver.findElement(By.xpath("//a[@href='/windows']"));
            text1.click(); //Navigates, no new window opened. Handle remains the same

            //WebElement 'Unnecessary code

            text2=driver.findElement(By.xpath("//a[@href='/windows/new']"));
            text2.click(); //opens second window/tab as per the settings. there are 2 window handles here for current driver instance
            Set<String> child=driver.getWindowHandles(); //set of 2
            System.out.println(child.size());
    //          ArrayList<String> children=new ArrayList<String>(child);' modifying this
            
             String strSecondWindowHandle = "";
             for(String str : s) // as set is not an ordered collection we need to loop through it to know which position holds which handle. 
            {
                if(str.equalsIgnoreCase(p1) == false) //to check if the window handle is not equal to parent window handle
                {
                         driver.switchTo().window(str) // this is how you switch to second window
                         strSecondWindowHandle = str;
                         break;
                }
            }
//            System.out.println(children);
//            driver.switchTo().window(children.get(1)); //not required
            System.out.println(driver.findElement(By.xpath("//div[@class='example']/h3")).getText());
            driver.close(); // now this will close the second window
            driver.switchTo().window(p1); // switches to main window
            System.out.println(driver.findElement(By.xpath("//div[@class='example']/h3")).getText());
//            driver.switchTo().window(""); //not required as it is the same window
            System.out.println(driver.getCurrentUrl());
            driver.close(); //closes the main window

所以回答你的问题

窗口句柄由操作系统 (Windows) 自动且唯一地分配给每个新打开的窗口

Q1 --> 除非您明确地切换窗口,否则窗口句柄保持不变。切换是这里的关键。

Q2 --> 导航不会改变句柄。它不是特定于页面的,而是特定于窗口的。 getWindowHandles 将返回当前正在运行的 WebDriver 实例打开的所有打开的浏览器窗口。不包括已打开的窗口。

Q3 --> 使用上面演示的 for 循环,您打开窗口,找到不是您的父窗口句柄的 ID,将其存储在变量中。对更多窗口重复此过程。

【讨论】:

  • 谢谢@Mithilesh Indurkar 的解释。这很有帮助:)
【解决方案2】:

您可以在documentation 中看到getWindowHandle()getWindowHandles() 方法之间的主要区别:

getWindowHandle():返回此窗口的不透明句柄,在此驱动程序实例中唯一标识它。

getWindowHandles():返回一组窗口句柄,通过将它们传递给switchTo().WebDriver.Options.window(),可用于迭代此 WebDriver 实例的所有打开窗口

简单来说,driver.getWindowHandles() 存储同时打开的所有页面的句柄集,但driver.getWindowHandle() 获取焦点网页的句柄。它获取活动浏览器的地址,返回类型为 String。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2018-02-24
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 2020-01-29
    相关资源
    最近更新 更多