2

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);

    }

}

6 Answers 6

3

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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)

2 Comments

One more doubt, is there any way to open that new window with a particular url, say facebook.com? TIA
There is nothing special, when you open new window you can simply use get() to serf to a particular url. And use switchTo().window(winHandle) to switch between windows.
1

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!

Comments

1

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.

Comments

0

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));
}

Comments

0
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);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.