How to Open Multiple Webpages in Separate Tabs Within a Browser Using Selenium-Webdriver and Python

Is there a way to open multiple tabs at once with Selenium + Python?

You can use send_keys with (Ctrl and T) to open new tab and then use get to enter the url you wish. Also you can track switch back and forth to all the tabs you have opened using window handles (driver.window_handles and driver.switch_to.window(handles)).

How to open multiple webpages in separate tabs within a browser using selenium-webdriver and python

To open multiple URLs / webpages in seperate TABs within a browser you can use the following solution :

  • Code Block :

    from selenium import webdriver

    first_page = "http://www.google.com"
    second_page = "https://www.facebook.com/"
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get(first_page)
    driver.execute_script("window.open('" + second_page +"');")
  • Browser Snapshot :

multiple_tabs

Open multiple URLs in same browser with selenium python

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

driver = webdriver.Chrome(executable_path=r"C:\Users\prave\Downloads\travelBA\chromedriver.exe")

driver.maximize_window()
driver.delete_all_cookies()

urls = ["https://business.google.com/u/0/edit/l/10199720925622488243?hl=fr",
"https://business.google.com/u/0/edit/l/13532588171385373346?hl=fr",
"https://business.google.com/edit/l/18307083220547614220",
"https://business.google.com/u/0/edit/l/08603059593698723407?hl=fr",
"https://business.google.com/edit/l/00810825496818981035"]
for posts in range(len(urls)):
print(posts)
driver.get(urls[posts])
if(posts!=len(urls)-1):
driver.execute_script("window.open('');")
chwd = driver.window_handles
driver.switch_to.window(chwd[-1])

// you can move to specific handle
chwd = driver.window_handles
print(chwd)

find the window handle and switch to it

how to open multiple windows in selenium at the same time. python

Try using Selenium 4 currently in Alpha only https://pypi.org/project/selenium/4.0.0.a7/

It provides the following for opening new windows and tabs:

# Opens a new tab and switches to new tab
driver.switch_to.new_window('tab')

# Opens a new window and switches to new window
driver.switch_to.new_window('window')

Python -- Opening multiple tabs using Selenium

try like this for python:

browser=webdriver.Chrome()
browser.get('http:/reddit.com')
window_before = driver.window_handles[0]
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)
time.sleep(3)
browser.get('http://bing.com')

Open web in new tab Selenium + Python

Editor's note: This answer no longer works for new Selenium versions. Refer to this comment.


You can achieve the opening/closing of a tab by the combination of keys COMMAND + T or COMMAND + W (OSX). On other OSs you can use CONTROL + T / CONTROL + W.

In selenium you can emulate such behavior.
You will need to create one webdriver and as many tabs as the tests you need.

Here it is the code.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.google.com/")

#open tab
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
# You can use (Keys.CONTROL + 't') on other OSs

# Load a page
driver.get('http://stackoverflow.com/')
# Make the tests...

# close the tab
# (Keys.CONTROL + 'w') on other OSs.
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 'w')

driver.close()

How to loop through multiple url and open urls in new tabs using selenium python chromedriver

You should be able to open all the details links in new tabs using the below logic.

driver.get("https://xxxxxx/blogs/")
if driver.find_element_by_xpath("(//span[@class='ui-datepicker-month'][contains(.,'May')])[1]"):
main_window = driver.current_window_handle
# get the number of details to click
addr = len(driver.find_elements_by_xpath("//a[@class='details'][contains(.,'Details')]"))
# iterate through all the details links (used the index rather elements list as it may lead to staleeleemnt exception after clicking on the first detiails link)
for addrNum in range(addr):
# get the details element based on index
ele = driver.find_element_by_xpath("(//a[@class='details'][contains(.,'Details')])[" + str (addrNum+1) + "]")
# get the href of the link
href = ele.get_attribute('href')
# open the href in another tab
driver.execute_script("window.open('" + href +"');")
# switching to parent window (on safer side)
driver.switch_to.window(main_window)

Opening more than two tabs with selenium

The issue happens due to using the same window name in both cases which is 'new window'. You may try using different window names in inventory and proton cases. Also note that window.open creates a browser window.



Related Topics



Leave a reply



Submit