Open Multiple Tabs in Selenium Python

Profile picture for user arilio666

You can use the following Python code to open many tabs in Selenium:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import urllib.request
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

driver.maximize_window()
driver.get('http://autopract.com/selenium/download.html')
driver.execute_script("window.open('about:blank','secondtab')")
driver._switch_to.window("secondtab")
driver.get('https://huntmcq.com')
print(driver.current_url)
  • We first create a new instance of the Chrome driver.
  • We then navigate to the first URL using the get() method.
  • To open a new tab, we use the execute_script command to open a blank window and name it. 
  • We then switch to new tab using the switch_to.window() method.
  • We repeat this process for each new tab we want to open and navigate to the corresponding URLs. 
  • We can switch between tabs using the switch_to.window() method and the window_handles property of the driver object.