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
# 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 neveropen site in 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
# 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 # 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 neveropen |
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 # Lets open https://www.bing.com/ in the second tab webBrowser.execute_script("window. open ( 'about:blank' , 'secondtab' );") webBrowser.switch_to.window( "secondtab" ) # Lets open https://www.facebook.com/ in the third tab 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
# 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 # Lets open https://www.bing.com/ in the second tab driver.execute_script("window. open ( 'about:blank' , 'secondtab' );") driver.switch_to.window( "secondtab" ) # Lets open https://www.facebook.com/ in the third tab driver.execute_script("window. open ( 'about:blank' , 'thirdtab' );") driver.switch_to.window( "thirdtab" ) # 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.