top of page
back.jpg

Multiple Tab Open Selenium-Python

crystgandhi

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()

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...

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
  • Facebook
  • Twitter
  • LinkedIn

©2019 by MGNK Junction - Junction to uplift your life

bottom of page