top of page
back.jpg

Multiple Tab Open Selenium-Python

What is the approach for dealing with multiple tabs in a web browser using Selenium in Python?


import time
from selenium import webdriver

# Create a new instance of the Chrome WebDriver
driver = webdriver.Chrome()

# Open the first tab and navigate to a website
driver.get("https://the-internet.herokuapp.com/")
time.sleep(3)
# Open a new tab using JavaScript (Ctrl+T shortcut)
driver.execute_script("window.open('', '_blank');")
time.sleep(3)
# Switch to the second tab
driver.switch_to.window(driver.window_handles[1])
driver.get("https://the-internet.herokuapp.com/windows")
print(driver.window_handles[1])

# Open another tab using JavaScript
driver.execute_script("window.open('', '_blank');")

# Switch to the third tab
driver.switch_to.window(driver.window_handles[2])
driver.get("https://the-internet.herokuapp.com/windows/new")
print(driver.window_handles[2])
# Close the third tab
driver.close()
# Switch back to the second tab
driver.switch_to.window(driver.window_handles[1])

# Perform actions in the second tab

# Close the remaining tabs
driver.close()
time.sleep(3)
driver.switch_to.window(driver.window_handles[0])
print(driver.window_handles[0])
driver.close()

# Quit the WebDriver
driver.quit()

7 views0 comments

Recent Posts

See All

Test case for Valid Coupon Code

This test case checks whether a valid coupon code is successfully applied to a booking cart, the total is updated accordingly, and a success message is displayed. package com.seleniumExamples2024; pu

Keyboard shortcut keys for various operations

General Shortcuts: Ctrl + C: Copy Ctrl + X: Cut Ctrl + V: Paste Ctrl + Z: Undo Ctrl + Y: Redo Ctrl + A: Select All Ctrl + S: Save Ctrl + P: Print Ctrl + F: Find Windows Shortcuts: Windows Key: Open or

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page