Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 340
Priyanshu Shekhar Avatar asked Jan 05 '17 09:01

Priyanshu Shekhar


People also ask

How do I open a new window in Google Chrome instead of a new tab?

To open a new window, use a keyboard shortcut: Windows & Linux: Ctrl + n. Mac: ⌘ + n.

How do I switch to a new window in Selenium?

Get the handles of all the windows that are currently open using the command: Set<String> allWindowHandles = driver. getWindowHandles(); which returns the set of handles. Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.

How do I force Chrome to open new links in a window instead of a tab?

Ctrl+click: open link in new tab. Shift+click: open link in new window.

What is the difference between opening a new tab and opening a new window?

Opening a new tab means a running application a web browser here opens a new partition in itself to perform new tasks. New window means a copy of the application running will start running and it also has ability to create new partition spaces for tabs.


1 Answers

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", "window.open('" + reference + "', '', 'width=800,height=600')")
document.querySelector('a#someID').removeAttribute('href')
// 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()
dr.get('https://login.live.com/login.srf?&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26realm%3dlogin.live.com')

dr.execute_script("""
    var reference = document.querySelector('a#ftrTerms').getAttribute('href');
    document.querySelector('a#ftrTerms').setAttribute("onclick", "window.open('" + reference + "', '', 'width=800,height=600')")
    document.querySelector('a#ftrTerms').removeAttribute('href')
    """)
link = dr.find_element_by_id('ftrTerms')
link.click()
like image 146
Andersson Avatar answered Sep 29 '22 21:09

Andersson