简体   繁体   中英

How to open multiple windows using Selenium WebDriver

I was trying to learn about "switching between multiple windows" using Selenium WebDriver but for that I am unable to OPEN multiple windows using driver.get() or driver.navigate.to(), that opens the links in the SAME window. Can someone help me to open multiple windows using the same driver instance? I have provided my sample code. That value of n is coming as 1 and not 2 as its opening in the same window. Please help.

public class MultipleWindows {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        WebDriver driver = new FirefoxDriver();

        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);


        driver.get("http://www.google.com");

        driver.navigate().to("http://www.facebook.com");


        int n = driver.getWindowHandles().size();
        System.out.println(n);

    }

}

This will also do

This will create a new tab/window and open the given url

String url="whatever url or empty to open a empty tab";
((JavascriptExecutor) driver).executeScript("window.open(arguments[0])", url);

WebDriver navigate().to() and get() do exactly the same thing . There is no API to open a new window, but you can use ctrl+n hotkey:

    WebDriver driver = new FirefoxDriver();
    WebElement body = driver.findElement(By.tagName("body"));
    body.sendKeys(Keys.chord(Keys.CONTROL, "n"));
    System.out.println(driver.getWindowHandles().size());
    driver.quit();

Or you can use few instances of WebDriver (see Selenium Java open new window, close it, and control main window again )

One more way from there

        WebDriver driver = new ChromeDriver();
        driver.get(adminToolURL);
        Set<String> windows = driver.getWindowHandles();
        String adminToolHandle = driver.getWindowHandle();
        ((JavascriptExecutor) driver).executeScript("window.open();");
        Set<String> customerWindow = driver.getWindowHandles();
        customerWindow.removeAll(windows);
        String customerSiteHandle = ((String) customerWindow.toArray()[0]);
        driver.switchTo().window(customerSiteHandle);
        driver.get(customerSiteURL);
        driver.switchTo().window(adminToolHandle);

Hope this helps!

I used clear way. I created driverA and driverB, than i can open 2 windows and operate on its without any mistake:

private WebDriver driverA;
private WebDriver driverB;

@BeforeClass(alwaysRun = true)
private void executeBeforeClass() {
    driverB = BrowserFactory.startBrowser(browser, baseUrl);
    driverA = BrowserFactory.startBrowserAsIncognito(browser, baseUrl);
}

@AfterClass(alwaysRun = true)
public void runAfterClass(){
    if(driverA!=null){
        driverA.quit();
    }
    if(driverB!=null){
        driverB.quit();
    }
}



@Test
public void shouldEhcacheWorkFine() {

    new LoginPage(driverA).loginToTomcatA(login, password)
            .goToListCompaniesPage()
            .goToEditFirstCompanyPage()
            .setPersonName(NAME);

    EditPage editPageB = new LoginPage(driverB).loginToTomcatB(login, password)
            .goToListCompaniesPage()
            .goToEditFirstCompanyPage();
    assertThat(editCompanyPageB.getPersonName(), is(NAME));
}

After call driver.get("http://www.google.com"); , to open new window please follow the following approach.

Use .sendKeys method:

String multipleKeys = Keys.chord(Keys.CONTROL, "t");
driver.findElement(By.tagName("body")).sendKeys(multipleKeys);

Use Actions :

Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();

*Note : For Mac OS please change Keys.CONTROL to Keys.COMMAND

Use JavascriptExecutor :

  • Open new blank window:
((JavascriptExecutor) driver).executeScript("window.open()");
  • Open new window with specific url:
((JavascriptExecutor) driver).executeScript("window.open('https://google.com')");

Hope this helps.

for (int i = 1; i < 10; i++) {
            ((JavascriptExecutor) driver).executeScript("window.open('https://www.google.com/')");
            ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
            driver.switchTo().window(tabs.get(i));
            Thread.sleep(2000);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM