How to auto switch browser tabs
Imagine you have a big monitor and you would like to display something from multiple web links, would it be nice if there is a way to auto switch between the multiple browser tabs in a fixed period? In this article, I will be sharing with you how to auto switch browser tabs via selenium, an automated testing tool.
There is a very detailed documentation on the python selenium library, you may want to check this document as the starting point. For this article, I will just walk through the complete code for this automation, so that you can use it as a reference in case you are tying to implement something similar.
Let’s get started!
To auto launch the browser, we need to first download the web driver for the browser. For instance, if you are using chrome browser, you may download the driver file here. Do check your browser version to make sure you download the driver for the correct version.
As the prerequisite, you will also need to run the below command to install the selenium package in your working environment.
pip install selenium
Launch the browser
Then import all the necessary modules into your script. For this article, we will need to use the below modules:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.common.exceptions import SessionNotCreatedException import time import os, sys
Let’s assume we want to display the below 3 links in your browser and make them auto switching between each other:
url_1 = "https://firstname.lastname@example.org,103.8403575,14z" url_2 = "https://weather.com/en-SG/weather/today" url_3 = "https://edition.cnn.com/"
Assuming you’ve already downloaded the chrome driver file and put it into the current script folder. Then let’s start to initiate the web driver to launch the browser:
options = Options() options.add_experimental_option('useAutomationExtension', False) try: driver = webdriver.Chrome(executable_path=os.getcwd() + "\\chromedriver.exe", options=options) except SessionNotCreatedException as e: print(e) print("please upgrade the chromedriver.exe from https://chromedriver.chromium.org/downloads") sys.exit(1)
You may wonder why we need a options parameter here? It’s actually optional, but you may see the “Loading of unpacked extensions is disabled by the administrator” warning without setting useAutomationExtension to False. There are plenty of other options to control the browser behavior, check here for the documentation.
As frequently you will see there is a new version of chrome, and it may not work with old driver file anymore. So, it’s better we catch this exception and show some error message to guide users to upgrade the driver.
You can set the chrome window position by doing the below, but it does not matter if you wish to maximize the window later.
Let’s open the first link and maximize our window (This also can be done by
#open window 1 driver.get(url_1) driver.maximize_window() driver.execute_script("document.body.style.zoom='120%'") time.sleep(1)
Next, we will open the second link. And for this tab, let’s scroll down 300px to skip the ads second at the page header.
#open second link driver.get(url_2) driver.execute_script("document.body.style.zoom='90%'") driver.execute_script("window.scrollBy(0,300);") time.sleep(1)
Similarly, we can open the third tab with the below code:
#open window 3 driver.execute_script("window.open('');") driver.switch_to.window(driver.window_handles) driver.get(url_3) driver.execute_script("document.body.style.zoom='90%'") driver.execute_script("window.scrollBy(0,200);") time.sleep(1)
Auto switch between tabs
Once everything is ready, we shall write the logic to auto switch between the different tabs at certain interval. To do that, we need to know how to perform the below 3 things:
- Identify what is the active link showing now
We can use driver.title attribute to check if the page title contains certain keyword for the particular website, so that we know which page is active now
- Switch to a new tab
We can continue to use driver.switch_to.window to switch the tab, but we need to have logic to determine which is the next tab we want to switch to
- Refresh the page (in case there is any updates)
We can use driver.refresh() to refresh the page, but we will lose the setting such as zooming in/out, so we need to set it again
So let’s take a look at the complete code:
nextIndex = 2 start = time.time() while True: #stop running after 5 minutes if (time.time() - start >= 5*60): break if "Google Maps" in driver.title: driver.refresh() driver.execute_script("document.body.style.zoom='120%'") time.sleep(3) nextIndex = 0 if nextIndex + 1 > 2 else nextIndex + 1 elif "CNN" in driver.title: driver.refresh() driver.execute_script("document.body.style.zoom='90%'") time.sleep(5) nextIndex = 0 if nextIndex + 1 > 2 else nextIndex + 1 elif "Weather" in driver.title: driver.refresh() driver.execute_script("document.body.style.zoom='90%'") time.sleep(2) nextIndex = 0 if nextIndex + 1 > 2 else nextIndex + 1 driver.switch_to.window(driver.window_handles[nextIndex])
So each of the tab will be active for a few seconds before switching to the next tab. And after 5 minutes, this loop will be stopped.
If we wish to close all tabs at the end of the script, we can perform the below:
for window in driver.window_handles: driver.switch_to.window(window) driver.close()
So that’s it and congratulations that you have completed a new automation project to auto switch browser tabs for Chrome. As per always, welcome any comments or questions.