使用Selenium Webdriver处理大量窗口弹出窗口时,应该优先选择哪个集合来处理,存储和迭代? [英] Which collection should be preferred to handle, store and iterate over a large number of window popups using selenium webdriver?

查看:78
本文介绍了使用Selenium Webdriver处理大量窗口弹出窗口时,应该优先选择哪个集合来处理,存储和迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以使用driver.getWindowHandles()处理窗口弹出窗口,然后将其存储在Set中,然后对其进行迭代的事实.但是,如果弹出窗口超过100个,该怎么办?

I am aware of the fact that we can handle window popups using driver.getWindowHandles() and then store it in a Set and then iterate over it. But what if there are more than 100 window popups?

考虑一种方案,其中用户导航到带有链接的网页.它单击此链接,将出现一个新的窗口弹出窗口,然后用户单击该弹出窗口上的链接,并再次出现另一个窗口弹出窗口,依此类推.最多100个或更多弹出窗口.

Consider a scenario where user navigates to a webpage with a link on it. It clicks on this link a new window popup appears and then user click on link on that popup and again another window popup appears and so on..upto 100 or more popups..

在这种情况下应首选哪个收藏集?或在这种情况下应采用哪种方法?

Which collection should be preferred in such case? or what approach should be applied in such a case?

推荐答案

getWindowHandles()

根据文档 getWindowHandles() 定义为:

java.util.Set<java.lang.String> getWindowHandles()

Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to switchTo().WebDriver.Options.window()

Returns:
    A set of window handles which can be used to iterate over all open windows.

因此,即使处理100个或更多的弹出窗口,您也很安全.

So even dealing with 100 or more popups also you are safe.

一个通用示例:

  • 代码块:

  • Code Block:

import java.util.Set;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WINDOW_HANDLE_ITERATE_Set_demo 
{
    public static void main(String[] args) throws Exception 
    {
        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        WebDriver driver =  new FirefoxDriver();
        driver.get("http://www.google.com");
        String parent_window = driver.getWindowHandle();
        System.out.println("Page Title is: "+driver.getTitle());
        ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
        new WebDriverWait(driver,10).until(ExpectedConditions.numberOfWindowsToBe(2));
        Set<String> allWindows_1 = driver.getWindowHandles();
        for(String hand1:allWindows_1)
        if(!parent_window.equals(hand1))
        {
            driver.switchTo().window(hand1);
            new WebDriverWait(driver,10).until(ExpectedConditions.titleContains("Face"));
            System.out.println("Page Title is: "+driver.getTitle());
            driver.close();
        }
        driver.switchTo().window(parent_window);
        System.out.println("Page Title is: "+driver.getTitle());
        driver.quit();
    }
}

  • 控制台输出:

  • Console Output:

    Page Title is : Google
    Page Title is : Facebook – log in or sign up
    Page Title is : Google
    

  • 但是,要在100个或更多的弹出窗口中导航,您还可以将 Set<String> 对象投射到 ArrayList<String> 对象中,如下所示:

    However to navigate through 100 or more popups you can also cast the Set<String> object into a ArrayList<String> object as follows:

    • 代码块:

    • Code Block:

    import java.util.ArrayList;
    import java.util.Set;
    
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class WINDOW_HANDLE_ITERATE_ArrayList_demo
    {
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver =  new FirefoxDriver();
            driver.get("http://www.google.com");
            System.out.println("Page Title is : "+driver.getTitle());
            String parent_window = driver.getWindowHandle();
            ((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
            WebDriverWait wait = new WebDriverWait(driver,5);
            wait.until(ExpectedConditions.numberOfWindowsToBe(2));
            Set<String> allWindows_1 = driver.getWindowHandles(); // this is the casting step
            ArrayList<String> tabs = new  ArrayList<>(allWindows_1);
            driver.switchTo().window(tabs.get(1));
            wait.until(ExpectedConditions.titleContains("Facebook"));
            System.out.println("Page Title is : "+driver.getTitle());
            driver.close();
            driver.switchTo().window(parent_window);
            System.out.println("Page Title is : "+driver.getTitle());
            driver.quit();
        }
    }
    

  • 控制台输出:

  • Console Output:

    Page Title is : Google
    Page Title is : Facebook – log in or sign up
    Page Title is : Google
    

  • List Set 是Java中几个 Collection 类中的两个.它们都用于存储对象,并为insertremoveretrieve elements提供方便的API,以支持通过 Collection 进行的迭代.

    List and Set are the two among several Collection classes in Java. Both of them are used to store objects and provides convenient APIs to insert, remove and retrieve elements to support Iteration over Collection.

      Java中的
    • List 允许重复,而 Set 不允许重复.如果insert Set 中重复,它将替换旧的值. Java中 Set 的任何实现都将仅包含唯一元素.
    • 列表是一个有序集合,而 Set 是一个无序集合. List 保持元素的插入顺序,这意味着之前插入的任何元素的索引都比后面插入的任何元素的索引低. Java中的 Set 不会保持任何顺序.尽管 Set 提供了另一种称为 SortedSet 的替代方案,它可以按照由中存储的对象的可比较方法和比较器方法定义的特定排序顺序存储Set元素.设置.
    • Java中 List 接口的
    • 受欢迎的实现包括 ArrayList Vector LinkedList .虽然 Set 界面的流行实现包括 HashSet TreeSet LinkedHashSet .
    • List in Java allows duplicates while Set doesn't allow any duplicate. If you insert duplicate in Set it will replace the older value. Any implementation of Set in Java will only contains unique elements.
    • List is an Ordered Collection while Set is an unordered Collection. List maintains insertion order of elements, means any element which is inserted before will go on lower index than any element which is inserted after. Set in Java doesn't maintain any order. Though Set provide another alternative called SortedSet which can store Set elements in specific Sorting order defined by comparable and comparator methods of Objects stored in Set.
    • Popular implementation of List interface in Java includes ArrayList, Vector and LinkedList. While popular implementation of Set interface includes HashSet, TreeSet and LinkedHashSet.

    在这里您可以找到有关

    Here you can find a reference about Difference between List and Set in Java Collection

    这篇关于使用Selenium Webdriver处理大量窗口弹出窗口时,应该优先选择哪个集合来处理,存储和迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆