Question: How to open new window instead of new tab in chrome using webdriver?


How to open new window instead of new tab in chrome using webdriver?

Answers 2
Added at 2017-01-05 10:01

In order to automate my test application, I need to open few links in a new window instead of tab. Keep this in mind that I am not opening the links in new tab explicitly, it is my web application which automatically lands user in the new tab after clicking on the link.

Why do I want to do this?

Because running the tests on chrome browser closes the main tab and keeps open the newly opened tab. Which ultimately fails the tests.So ultimate intention is to open the new window instead of tab and handle it properly using driver.getWindowHandles().

What have I done so far?

I tried to find some kind of capability setting or profile in Chrome which automatically opens the links in a new window which are supposed to be open in a tab.But did not find any convincing solution most of the suggestions are CTRL+CLICK ON THE LINK.

Answers to

How to open new window instead of new tab in chrome using webdriver?

nr: #1 dodano: 2017-01-05 12:01

I'm not a guru of web-design, but I can suggest following scenario:

// Get required page
// Excecute below JavaScript with JavaScriptExecutor
var reference = document.querySelector('a#someID').getAttribute('href'); // You can use your specific CSS Selector instead of "a#someID"
document.querySelector('a#someID').setAttribute("onclick", "'" + reference + "', '', 'width=800,height=600')")
// Find target link
// Click on it

This code should allow you to make changes in HTML source code of target web-element to force its opening in new browser window.

Note that with this code element's appearance on page will be changed until page refresh

P.S. You didn't mentioned your programming language, so there is no complete implementation... However, this is Python implementation example:

from selenium import webdriver as web

dr = web.Chrome()

    var reference = document.querySelector('a#ftrTerms').getAttribute('href');
    document.querySelector('a#ftrTerms').setAttribute("onclick", "'" + reference + "', '', 'width=800,height=600')")
link = dr.find_element_by_id('ftrTerms')
nr: #2 dodano: 2017-01-05 13:01

Well, in the absence of any flag/setting/capability in Chrome browser which opens the links in a new window instead of the new tab I used a Chrome Extension for that via WebDriver.

Why did I do that?

Because my tests are running fine on firefox and I have no idea how many WebElements are there in the suite which gets open in new tab in Chrome browser. The suite is also very huge so doing any changes in its core page class may break the all the tests.In addition to that, changing code at an element level will be very time-consuming and most importantly not a generic solution.

What did I do?

  1. I used a chrome extension New Tab New Window, which opens all the new tabs into a new window.
  2. Downloaded the CRX file of this extension using an extension Get CRX.
  3. Set the CRX file as a capability of Chrome.

ChromeOptions options = new ChromeOptions(); options.addExtensions(new File("pathOfCRXFile")); DesiredCapabilities capabilities =; capabilities.setCapability(ChromeOptions.CAPABILITY, options); WebDriver driver = new ChromeDriver(capabilities);

So above will convert all the new tabs into a new window.So whenever the driver clicks on any link which further opens in a new tab will get opened into the new window.

Source Show
◀ Wstecz