Python - Opening multiple tabs using Selenium
Last Updated :
27 Mar, 2023
Testing is an important concept in software methodology. Software is said to be effective and efficient only if it is bug-free. Testing can be done manually and also via automation. In Python, selenium is used to do automated testing. The selenium package is available, and they are much helpful to automate web browser interaction from Python.
In this article, we are going to discuss how to open multiple tabs using selenium.
Installation
pip install selenium
Selenium Driver requirements for different browsers :
Each browser is different and similarly, they require different selenium webdrivers.
Popular browsers like Chrome, Firefox etc., and their webdriver download path is given below
Chrome: https://sites.google.com/a/chromium.org/chromedriver/downloads
Edge: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox: https://github.com/mozilla/geckodriver/releases
Safari: https://webkit.org/blog/6900/webdriver-support-in-safari-10
To begin with, let us see a normal example of opening a firefox browser
Steps required:
Python3
# Necessary webdrivers need to be imported
from selenium import webdriver
# This is for Firefox. Similarly if
# chrome is needed , then it has to be specified
webBrowser = webdriver.Firefox()
# This will open geeksforgeeks site in Firefox
webBrowser.get('https://www.geeksforgeeks.org/')
On execution of the code, we can see the actions that is shown below:

Ways to open multiple tabs using Selenium:
- After specifying either Firefox/Chrome driver for selenium, first, we need to open a webpage
- We need to call "execute_script" method which in turn executes window.open('about:blank', 'secondtab') javascript.
- Then we need to switch to that tab and for that tab can give any valid URL.
Python3
# Necessary webdrivers ned to be imported
from selenium import webdriver
# This is for Firefox. Similarly if
# chrome is needed , then it has to be specified
webBrowser = webdriver.Firefox()
# first tab. Open google.com in the first tab
webBrowser.get('http://google.com')
# second tab
# execute_script->Executes JavaScript snippet.
# Here the snippet is window.open that means, it
# opens in a new browser tab
webBrowser.execute_script("window.open('about:blank',
'secondtab');")
# It is switching to second tab now
webBrowser.switch_to.window("secondtab")
# In the second tab, it opens geeksforgeeks
webBrowser.get('https://www.geeksforgeeks.org/')
Output:
The very same above program can be run by using the chrome driver as well. Chrome driver is version specific and hence respective version to your chrome browser, we need to download

A small change in the above code is instead of "webdriver.Firefox()" , we should have webdriver.Chrome()
Let us see how to open 3 tabs using chrome driver now
Python3
# Necessary webdrivers ned to be imported
from selenium import webdriver
# Get the instance of the webBrowser
# window, here we are using Chrome
webBrowser = webdriver.Chrome()
# Lets open google.com in the first tab
webBrowser.get('http://google.com')
# Lets open https://www.bing.com/ in the second tab
webBrowser.execute_script("window.open('about:blank',
'secondtab');")
webBrowser.switch_to.window("secondtab")
webBrowser.get('https://www.bing.com/')
# Lets open https://www.facebook.com/ in the third tab
webBrowser.execute_script("window.open('about:blank',
'thirdtab');")
webBrowser.switch_to.window("thirdtab")
webBrowser.get('https://www.facebook.com/')
On execution of the scripts, we can see:

Let us check how to specify different drivers by taking browser name as input and open multiple tabs
Python3
# Necessary imports
from selenium import webdriver
# initially webdriver is empty
webdriver.driver = None
browserName = input("Enter your browser name(chrome/firefox/edge/ie):")
# Depends upon the browser name, drivers are selected,
# in order to check for all given 4 browser checkings,
# all 4 drivers must be installed and they should be
# available in "Path"
if browserName.upper() == "CHROME":
driver = webdriver.Chrome()
elif browserName.upper() == "FIREFOX":
driver = webdriver.Firefox()
elif browserName.upper() == "EDGE":
# MicrosoftWebDriver.exe should be
# downloaded and available in Path
driver = webdriver.Edge()
elif browserName.upper() == "IE":
# IEDriverServer.exe should be
# downloaded and available in Path
driver = webdriver.Ie()
else:
print("No browser is specified")
# Lets open google.com in the first tab
driver.get('http://google.com')
# Lets open https://www.bing.com/ in the second tab
driver.execute_script("window.open('about:blank',
'secondtab');")
driver.switch_to.window("secondtab")
driver.get('https://www.bing.com/')
# Lets open https://www.facebook.com/ in the third tab
driver.execute_script("window.open('about:blank',
'thirdtab');")
driver.switch_to.window("thirdtab")
driver.get('https://www.facebook.com/')
# It is always good to quit the driver
# driver.quit()
Selenium testing is getting applied in all software industries. It is quicker and efficient. Manual errors also got overcome by means of selenium testing. As everywhere automation is happening, testing also getting done in automated ways only nowadays. Opening multiple tabs for checking different functionalities is a common task.
Similar Reads
Python - Opening links using Selenium
Selenium is a powerful tool for controlling the web browser through the program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. Selenium Python bindings provide a convenient API to a
2 min read
Opening and Closing Tabs Using Selenium
Selenium is a tool which is used to automate browser instructions. It is utilitarian for all programs, deals with all significant OS and its contents are written in different languages i.e Python, Java, C# etc. In this article, we are using Python as the language and Chrome as the WebDriver. Instal
2 min read
How to scrape multiple pages using Selenium in Python?
As we know, selenium is a web-based automation tool that helps us to automate browsers. Selenium is an Open-Source testing tool which means we can easily download it from the internet and use it. With the help of Selenium, we can also scrap the data from the webpages. Here, In this article, we are g
4 min read
How to get current_url using Selenium in Python?
While doing work with selenium many URL get opened and redirected in order to keeping track of URL current_url method is used. The current_url method is used to retrieve the URL of the webpage the user is currently accessing. It gives the URL of the current webpage loaded by the driver in selenium.
2 min read
Locating single elements in Selenium Python
Locators Strategies in Selenium Python are methods that are used to locate elements from the page and perform an operation on the same. Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provide a simple API to write functional/acceptance tests using
5 min read
Get all text of the page using Selenium in Python
As we know Selenium is an automation tool through which we can automate browsers by writing some lines of code. It is compatible with all browsers, Operating systems, and also its program can be written in any programming language such as Python, Java, and many more. Selenium provides a convenient A
3 min read
How to Add Chrome Extension using Python Selenium
IntroductionSelenium is a tool for browser automation that supports multiple browsers for testing. With webdriver we can use multiple languages like Python, C#, and Java to write the code for automation testing. For Adding a Chrome extension in a browser with Selenium WebDriver allows us to automate
5 min read
How to Locate Elements using Selenium Python?
Selenium: is an open-source tool that automates web browsers. It provides a single interface that lets you write test scripts in programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C#, among others. I personally prefer Python as itâs very easy to write code in python. A browser-dri
3 min read
Upload File With Selenium in Python
Uploading files using Selenium in Python allows developers to automate the process of interacting with file input elements on web pages. Leveraging the Selenium WebDriver, this tutorial guides users through the steps of locating file input elements, providing the file path for upload, and handling t
2 min read
Google Maps Selenium automation using Python
Prerequisites: Browser Automation using Selenium Selenium is a powerful tool for controlling a web browser through the program. It is functional for all browsers, works on all major OS, and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. It can
4 min read