How To Switch Tabs In A Browser Using Selenium Python?

This page summarizes the projects mentioned and recommended in the original post on dev.to

InfluxDB - Power Real-Time Data Analytics at Scale
Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
www.influxdata.com
featured
SaaSHub - Software Alternatives and Reviews
SaaSHub helps you find the best software and product alternatives
www.saashub.com
featured
  • php-selenium-sample

    Run PHP and Selenium scripts on LambdaTest automation cloud. A sample repo to help you run PHP based test scripts in parallel with LambdaTest

  • import unittest from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains import time from selenium.webdriver.common.keys import Keys class SwitchTab(unittest.TestCase): def setUp(self): # self.driver = webdriver.Firefox() self.driver = webdriver.Chrome(chrome webdriver executable location) def test_switch_tab(self): driver = self.driver driver.maximize_window() driver.get('https://www.lambdatest.com/') time.sleep(5) # collects handle ID of current window first_tab_handle = driver.current_window_handle print("first_tab_handle : "+str(first_tab_handle)) # locates a link element on the loaded url using xpath new_tab_link = driver.find_element_by_xpath('//a[contains(@class,"nav-link") and contains(@href,"selenium-automation")]') time.sleep(5) action = ActionChains(driver) # clicks on the located link element with CONTROL button in pressed state using actionChains class. This opens the link in a new tab. action.key_down(Keys.CONTROL).click(new_tab_link).key_up(Keys.CONTROL).perform() time.sleep(3) print("driver.window_handles : " + str(driver.window_handles)) print("current window handle : "+ str(driver.current_window_handle)) if driver.current_window_handle == first_tab_handle: print("driver focus is not switched to new tab opened using actionChains.") else: print("window handle has changed. Driver switched focus to new tab.") driver.switch_to.window(driver.window_handles[1]) time.sleep(3) # stores handle of tab opened using actionchains if driver.current_window_handle != first_tab_handle: ctrl_click_tab = driver.current_window_handle print("driver focus switched. New tab's handle id is - ") print(ctrl_click_tab) else: print("driver focus is not shifted to new tab.") time.sleep(5) driver.execute_script('''window.open("", "_blank");''') time.sleep(5) print("driver.window_handles : " + str(driver.window_handles)) try: if (driver.current_window_handle == first_tab_handle) or (driver.current_window_handle == ctrl_click_tab): print("Though, this tab seems to be an active window as it's highlighted but driver control still remains with the tab we last switched to.") except: pass time.sleep(3) for handle in driver.window_handles: if (handle == first_tab_handle) or (handle == ctrl_click_tab): print(handle) else: js_tab_handle = handle print("js tab handle is -") print(js_tab_handle) driver.switch_to.window(js_tab_handle) time.sleep(5) break if driver.current_window_handle == js_tab_handle: print("driver focus shifted to js tab with handle id -") print(driver.current_window_handle) driver.get('https://www.lambdatest.com/blog/') # shifts control or focus to the first window in window handles, it's not the last opened tab but 1st tab in order. driver.switch_to_window(driver.window_handles[1]) time.sleep(5) driver.get('https://www.lambdatest.com/pricing') driver.switch_to.window(driver.window_handles[-1]) time.sleep(5) driver.get('https://www.lambdatest.com/newsletter/') time.sleep(3) driver.switch_to.window(first_tab_handle) if driver.current_window_handle == first_tab_handle: print("current_window_handle : " + str(driver.current_window_handle)) print("driver switched to first tab") else: print("driver switching failed") time.sleep(5) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()

  • InfluxDB

    Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.

    InfluxDB logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts