Created
March 22, 2024 10:35
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver.common.by import By | |
import time | |
# Create the Chrome WebDriver. | |
chrome_driver = webdriver.Chrome() | |
# Function to open multiple tabs. | |
def open_multiple_tabs_from_urls(urls): | |
# Open the first URL in the first tab. | |
chrome_driver.get(urls[0]) | |
# Print the title of the first tab. | |
print("Opened Tab = " + chrome_driver.title) | |
# Add a time delay for better visibility and observability. | |
time.sleep(1) | |
# Open new tabs for all the remaining URLs. | |
for url in urls[1:]: | |
# This javascript is executed to open a new tab. | |
chrome_driver.execute_script(f"window.open('{url}', '_blank');") | |
# Switch to the new tab. | |
chrome_driver.switch_to.window(chrome_driver.window_handles[-1]) | |
# Print title of new tab. | |
print("Opened Tab = " + chrome_driver.title) | |
time.sleep(1) | |
# Function to close all tabs except the current tab | |
def close_all_except_current_tab(): | |
# Get all tab lists and current tab using the | |
# window_handles and current_window_handle method. | |
all_tab_list = chrome_driver.window_handles | |
current_tab = chrome_driver.current_window_handle | |
# Print Current Tab name. | |
print("Current Tab = " + chrome_driver.title) | |
# Iterating over the open tabs. | |
for window in all_tab_list: | |
# Condition to make sure current tab is not closed. | |
if window != current_tab: | |
chrome_driver.switch_to.window(window) | |
print("Closing Tab = " + chrome_driver.title) | |
# close() method is used to close the selected tab. | |
chrome_driver.close() | |
# Switch back to the current tab | |
chrome_driver.switch_to.window(current_tab) | |
# URL list to open in tabs | |
urls = [ | |
"https://ecommerce-playground.lambdatest.io/", | |
"https://ecommerce-playground.lambdatest.io/index.php?route=product/special", | |
"https://ecommerce-playground.lambdatest.io/index.php?route=extension/maza/blog/home" | |
] | |
# Open multiple tabs | |
open_multiple_tabs_from_urls(urls) | |
# Close all tabs except the current tab | |
close_all_except_current_tab() | |
# Close the browser session | |
chrome_driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment