How can selenium web driver get to know when the new window has opened and then resume its execution

后端 未结 7 581
情深已故
情深已故 2020-12-03 07:05

I am facing an issue in automating a web application using selenium web driver.

The webpage has a button which when clicked opens a new window. When I use the follow

7条回答
  •  一生所求
    2020-12-03 07:25

    Below function can wait for given max time until your new window is open

    public static void waitForWindow(int max_sec_toWait, int noOfExpectedWindow) {
        FluentWait wait = new FluentWait(driver);
        wait.pollingEvery(Duration.ofMillis(200));
        wait.withTimeout(Duration.ofSeconds(max_sec_toWait));
        wait.ignoring(NoSuchWindowException.class);
        Function function = new Function(){
            @Override
            public Boolean apply(WebDriver driver) {
                Set handel = driver.getWindowHandles();
                if(handel.size() == noOfExpectedWindow) 
                    return true;
                else 
                    return false;
                }
        };      
        wait.until(function);
    }
    

提交回复
热议问题