Switching windows or tabs is also possible from Python code. The example below uses the selenium module and web driver.

This should work for all the supported web browsers including Chrome, Firefox, IE and all the others.

Related course:

switch to window

selenium switch to window

The selenium switch to window code shown below. It starts firefox, opens a webpage, then a new tab and window with different web sites.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# -*- coding: utf-8 -*-
from selenium import webdriver
import time

browser=webdriver.Firefox()
browser.get("https://www.reddit.com")
browser.execute_script("window.open()")
print(browser.window_handles)
browser.switch_to_window(browser.window_handles[1])
browser.get("https://www.youtube.com")
time.sleep(1)
browser.switch_to_window(browser.window_handles[0])
browser.get("https://python.org")
#browser.close()

selenium switch to window

Download examples