Skip to main content

How to open a new tab in a browser using Selenium WebDriver?

TL;DR:This is a story. If a story does not fascinate you, jump to the solution.

Prologue:-
If you constantly monitor many forums for some reason like LinkedIn groups, Google groups, Stackoverflow for Selenium related queries, you will find a question that has been asked many times with the same answer. Yes, I’m talking about the below question -

How to open a new tab in a browser using Selenium WebDriver?

Or this might have been asked in different style like -

Open new tab instead of a new window using Selenium WebDriver


You will easily find an answer after quick Google search. Why this post? 
           Because I do not agree with the most accepted answer of this question. I am not saying that solution provided is wrong or it won't work, I am just saying that I disagree with the solution provided. What is the solution provided by the way -  Here starts our story.


Open a blank tab in a browser using Selenium WebDriver


And very obvious solution would be use of sendKeys 



           Now the above solution uses shortcut keys provided by the browser to open a new tab. Generally, all browsers use same keys to open a new tab. BUT shortcut keys are OS specific. Being said that solution provided is not platform agnostic. If you want to open the new tab on Macintosh / Mac OS, you have to send  COMMAND+‘t’  together so your code would be like - 


            So if think you have solved the problem straight away using sendKeys, think again. I mostly use windows OS and I estimate my effort based on the amount of time needed to make the code work on windows as selenium is platform agnostic but using such approaches we need to spend additional time on another platform. This is a lesson learned in a hard way. So if you still want to use send keys approach with both OS [which is mostly the case with web application] , your code would look like this-



            This is not very efficient code, every time you want to open a new tab you have to check OS and then you add the complexity of If else to open a new tab.

Solution:-   
            Why not use javascript? All major browsers support javascript. Use javascript to open a new tab in Selenium WebDriver. Simple & elegant solution.



Bonus:- Open URL in a new tab in Selenium WebDriver.

Sometimes you might need to open URL in a new tab which you evaluated after some operation in a browser. All you need is send a parameter to javascript as below.


This is all for now.
Cheers!!

Comments

Popular posts from this blog

How to Open Browser in Incognito / InPrivate mode using Selenium / WebDriver

How to Open Chrome Browser in Incognito mode using Selenium / WebDriver ?          We will use Chrome Drivers Capabilities & ChromeOptions to open Chrome browser in incognito mode. To be precise, we have to use argument --incognito  for ChromeOption as shown in below example- How to Open Firefox Browser in Incognito / Private mode using Selenium / WebDriver ?     We will use Firefox Profile   to open Firefox in private mode. To be precise, we will set browser.private.browsing.autostart in firefox profile preference. How to Open Internet Explorer (IE) Browser in InPrivate mode using Selenium / WebDriver ?       We will use IE Driver Capabilities to open IE in InPrivate mode. To be precise we will use FORCE_CREATE_PROCESS capability along with IE_SWITCHES to which parameter would be -private This is all for now. Cheers!!

History Of Selenium WebDriver

I have assumed that you have already read my past blog entry, Introduction to Selenium WebDriver and here on the quest to find out technical answer of question - what selenium is? Before getting to the answer, I want you to go though the below info-graphics of history of Selenium WebDriver.  History of Selenium Now that you are aware of history, Can you answer what Selenium is? Selenium is set of tools which includes :- Selenium IDE Selenium RC WebDriver Selenium Grid So meaning of Selenium depends upon the context, c iting the man himself - Simon Stewart here - Unfortunately, the Selenium project uses a lot of jargon. Selenium Core is the heart of the original Selenium implementation, and is a set of Javascript scripts that control the browser. This is sometimes referred to as "Selenium" and sometimes as "Core". Selenium RC was the name given to the language bindings for Selenium Core, and is commonly, and confusingly, referred to as just "Selenium"