Switch between two browser windows using Selenium WebDriver

后端 未结 2 485
暗喜
暗喜 2020-12-03 12:10

I used Firefox Driver to open two URL\'s. Whenever I invoke driver, new firefox window is opened. I have to switch between these two windows. How can I do this?

2条回答
  •  囚心锁ツ
    2020-12-03 12:33

    you can use following code to switch between windows based on the window title

     private void handleMultipleWindows(String windowTitle) {
                Set windows = driver.getWindowHandles();
    
                for (String window : windows) {
                    driver.switchTo().window(window);
                    if (driver.getTitle().contains(windowTitle)) {
                        return;
                    }
                }
            }
    

    Similary you could use URL or some other criteria to switch windows.

提交回复
热议问题