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:
- We need Geckodriver to open firefox browser. It can be downloaded from https://github.com/mozilla/geckodriver/releases. It has to be set in “Path” variable in windows and also in Linux and Mac, in respective locations it has to be set.
- Open a new Firefox browser from Python.
- Then load the page at the given valid URL.
Python3
from selenium import webdriver
webBrowser = webdriver.Firefox()
|
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
from selenium import webdriver
webBrowser = webdriver.Firefox()
webBrowser.execute_script("window. open ( 'about:blank' ,
'secondtab' );")
webBrowser.switch_to.window( "secondtab" )
|
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
from selenium import webdriver
webBrowser = webdriver.Chrome()
webBrowser.execute_script("window. open ( 'about:blank' ,
'secondtab' );")
webBrowser.switch_to.window( "secondtab" )
webBrowser.execute_script("window. open ( 'about:blank' ,
'thirdtab' );")
webBrowser.switch_to.window( "thirdtab" )
|
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
from selenium import webdriver
webdriver.driver = None
browserName = input ( "Enter your browser name(chrome/firefox/edge/ie):" )
if browserName.upper() = = "CHROME" :
driver = webdriver.Chrome()
elif browserName.upper() = = "FIREFOX" :
driver = webdriver.Firefox()
elif browserName.upper() = = "EDGE" :
driver = webdriver.Edge()
elif browserName.upper() = = "IE" :
driver = webdriver.Ie()
else :
print ( "No browser is specified" )
driver.execute_script("window. open ( 'about:blank' ,
'secondtab' );")
driver.switch_to.window( "secondtab" )
driver.execute_script("window. open ( 'about:blank' ,
'thirdtab' );")
driver.switch_to.window( "thirdtab" )
|
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.
Share your thoughts in the comments
Please Login to comment...