In this post, we are going to learn how to open or close a tab in Selenium Python with examples. The selenium Module is used for automation testing in Python. Python Selenium API provides the functionality to write tests using the Selenium web driver. The selenium driver provides all web browser driver implementation for Firefox, Chrome, IE, and opera.
1. How to open tab in Selenium Python
To open a tab or navigate to a link in Python Selenium API, the get(‘https://www.google.com’) method is used that takes a link on which we want to navigate as an argument and waits until the page is fully loaded.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
brodriver= webdriver.Chrome(ChromeDriverManager().install())
brodriver.maximize_window()
brodriver.get("https://accounts.google.com/")
print("The tab we have opened = " + brodriver.title)
Output
The tab we have opened = Sign in - Google Accounts
- How to Install selenium on windows Python
- How to install selenium in VSCode
- How to open chrome browser in selenium Python
- How to open Edge browser in selenium Python
- How to open Firefox browser in selenium Python
- Executable path has been deprecated in selenium
- Solved version of ChromeDriver only supports Chrome version 98
- Solved selenium find_element_by_* commands are deprecated
- How to open new tab in Selenium Python
- How to open new window Selenium Python
2. How to open close tab in Selenium Python switch windows
To open a tab or navigate to a link in Python Selenium API, get(‘https://www.google.com’) method is used, Whereas open a new tab in a Window Javascript window. the open() method is used. We execute the javascript function by Python Selenium API execute_script() method. The web driver supports moving between windows by using switch_to.window() method, window_handles: returns handles of all windows in the current session We pass the window handle as a parameter to switch_window() to switch between the specific windows.
In this example, we will understand how to open and close a tab in selenium python without closing the browser.
- First, we will navigate to “https://accounts.google.com/”
- We will open a new tab by using “Brodriver.execute_script(“window. open(”);”)” and wait for 2 Second and close the tab by calling the driver.close() method
- We will switch to the first tab by using Brodriver.switch_to.window(Brodriver.window_handles[0]) and navigate to yahoo.com
- Finally, close the browser by calling the driver.quit() method all windows related to driver instance will quit.
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time
Brodriver = webdriver.Chrome(ChromeDriverManager().install())
Brodriver.maximize_window()
Brodriver.get("https://accounts.google.com/")
print("The tab we have opened = " + Brodriver.title)
Brodriver.execute_script("window.open('');")
Brodriver.switch_to.window(Brodriver.window_handles[1])
Brodriver.get("https://www.duckduckgo.com")
print("Second Title = " + Brodriver.title)
time.sleep(2)
Brodriver.close()
#switching to old tab
Brodriver.switch_to.window(Brodriver.window_handles[0])
Brodriver.get("https://www.yahoo.com")
print('Third tab we opened:',Brodriver.title)
time.sleep(2)
Brodriver.quit()
Output
The tab we have opened = Sign in - Google Accounts
Second Title = DuckDuckGo — Privacy, simplified.
Third tab we opened: Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos
3. How to open and close tab in Selenium Python
In this python selenium example, we will learn how to open and close a tab in Selenium Python. To open a tab or navigate to a link in Python Selenium API, get(‘https://www.google.com’) method is used that takes a link on which we want to navigate as an argument and waits until the page is fully loaded.
- Close():close a current window in browser.
- driver.quit() :All window opened in driver instance will close
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
brodriver= webdriver.Chrome(ChromeDriverManager().install())
brodriver.maximize_window()
brodriver.get("https://accounts.google.com/")
print("The tab we have opened = " + brodriver.title)
#close a current window in browser
driver.close()
#All window opened in driver instance will close
driver.quit()