简体   繁体   中英

Multiple Browser WebDriver Selenium

I am working on a Selenium test project where I need to launch two browsers at initial setup .

Then I need to do switching between these browsers.

So I will have [Window1] [Window2]

I would like to run test through [Window1] and then switch to [Window2] to check result of actions done in [Window1]

Any idea on how to do it?

I tried driver.switchTo().window() but no luck.

Any help would be greatly appreciated. Thanks

driver.switchTo().window() will work only if new window is opened by any action in existing window. If you are using different drivers to open different windows then it wont work.
In such case you need to choose appropriate instance of driver to control the new window.

Suppose you have instance of webdriver

// Window 1
WebDriver chrome = new ChromeDriver()

// Window 2
WebDriver firefox = new FirefoxDriver()

Now use chrome whenever you want to interact with Window 1 and use firefox to interact with Window 2.

Just use two driver instancess:

WebDriver driver1 = new ChromeDriver()
WebDriver driver2 = new FirefoxDriver()

You can make them both same flavour if you want.

You need to pass the parameter as window name or you can get all the window handles and then switch to the particular window handle.

You could use:

driver.switchTo().window("windowName");

or:

for (String handle : driver.getWindowHandles()) {
    driver.switchTo().window(handle);
}

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