Open and close tabs in browser using Selenium Python
Selenium is a powerful web automation and testing tool. We use Selenium to write scripts, which can control the web browser and perform specific actions.
In this guide, we will use Python to write a script that will automatically open and close websites in new tabs.
Install Selenium and Chrome WebDriver
To install Selenium, we use the following command.
#Python 3.x
pip install selenium
ChromeDriver is another executable file that Selenium WebDriver uses to interact with Chrome. If we want to automate tasks on the Chrome web browser, we also need to install ChromeDriver.
Depending on the version of Chrome browser, we need to select a compatible driver for it. Here are the steps to install and configure the Chrome driver:
- Click this link. Download the Chrome driver based on your Chrome browser version and operating system type.
- If you want to find the version of the Chrome browser, click the three dots in the upper right corner of Chrome, click Help, and select About Google Chrome. You can check the Chrome version in the about section.
- Extract the zip file and run the Chrome driver.
Opening tabs in a browser using Selenium Python
We create a WebDriver instance in the following code and specify the path to the Chrome driver. We then get()
set the URL of the target website using the method with the driver instance.
It will open the target website in the Chrome browser.
Sample code:
# Python 3.x
from selenium import webdriver
driver = webdriver.Chrome(r"E:\download\chromedriver.exe")
driver.get("https://www.verywellmind.com/what-is-personality-testing-2795420")
Output:
Open a new tab in your browser using Selenium Python
To open a new tab in the same browser window, we will use the JavaScript executor. It execute_script()
executes JavaScript commands using the method.
We pass the JavaScript command as a parameter to this method. We will use window.open()
the command to open another tab in the window.
The window handle stores the unique address of a window opened in a web browser. switch_to_window()
The method switches to the specified window address.
1
Represents the address of the second window. Finally, we'll use get()
the method to provide the URL of the new website.
Sample code:
# Python 3.x
from selenium import webdriver
driver = webdriver.Chrome(r"E:\download\chromedriver.exe")
driver.get("https://www.verywellmind.com/what-is-personality-testing-2795420")
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get(
"https://www.indeed.com/career-advice/career-development/types-of-personality-test"
)
Output:
Close a browser tab using Selenium Python
We will use the method in our driver program close()
to close the tab.
Sample code:
# Python 3.x
from selenium import webdriver
driver = webdriver.Chrome(r"E:\download\chromedriver.exe")
url = "https://www.16personalities.com/free-personality-test"
driver.get(url)
driver.close()
Close one tab and switch to another in the browser using Selenium Python
In the following code using Selenium, we have opened a URL in a tab. We open another tab and switch_to.window(driver.window_handles[1])
switch to it using .
A new tab will open with the specified URL. We will now close()
close this tab using the method and switch_to.window(driver.window_handles[0])
switch back to the previous tab using the method.
Sample code:
# Python 3.x
from selenium import webdriver
driver = webdriver.Chrome(r"E:\download\chromedriver.exe")
url = "https://www.16personalities.com/free-personality-test"
driver.get(url)
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get("https://www.16personalities.com/personality-types")
driver.close()
driver.switch_to.window(driver.window_handles[0])
Output:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Enumerating a dictionary in Python
Publish Date:2025/05/05 Views:98 Category:Python
-
The function in Python enumerate() returns an object of enumeration type and adds a counter variable to iterate over a list or other type of collection. It makes looping over such objects easier. When we pass an enumeration object to list()
Changing dictionary values in Python
Publish Date:2025/05/05 Views:108 Category:Python
-
This tutorial will discuss various ways to change the value of a particular key in Python dictionary. We can do this by using the following methods. dict.update() method for cycle. Dictionary Unpacking dict.update() How to change dictionary
Finding the maximum value in a Python dictionary
Publish Date:2025/05/05 Views:60 Category:Python
-
This tutorial explains how to get a key with the maximum value in Python. Since the method has changed from the previous Python versions, it also lists some sample codes to clarify the concepts. Use operator.itemgetter() the method to get t
How to read input from stdin in Python
Publish Date:2025/05/05 Views:124 Category:Python
-
This tutorial discussed stdin the methods of reading input from in Python. You can read directly from the console or from a file name specified in the console. In Python, fileinput.input() use stdin fileinput We can use the read module in P
Maximum integer in Python
Publish Date:2025/05/05 Views:55 Category:Python
-
This tutorial will discuss the maximum integer value in different versions of Python and how we can get it. In Python 2, integers and long integers are different data types. The maximum value of an integer is 2 31 -1. If the value exceeds t
Get a list of time zones using Python
Publish Date:2025/05/05 Views:107 Category:Python
-
When developing real-world applications, software developers must ensure that the application can support users from both their own country and other parts of the world. Since most countries have different time zones and many people around
Convert NumPy array to list in Python
Publish Date:2025/05/05 Views:101 Category:Python
-
Lists and arrays are the two most basic and commonly used collection objects in Python. They are both mutable and are used to store a collection of elements under a common name and each element has a specific location that can be used to ac
Appending 2D Arrays in Python
Publish Date:2025/05/05 Views:64 Category:Python
-
In Python, we can have ND arrays. We can use NumPy module to process arrays in Python. This tutorial demonstrated the different methods you can use to append values to a two-dimensional array in Python. Use append() the function to ap
Sliding average of NumPy arrays in Python
Publish Date:2025/05/05 Views:190 Category:Python
-
The sliding average is often used to study time series data by calculating the average of data at a specific time interval. It is used to eliminate some short-term fluctuations and study data trends. When studying stock price trends, the si