Skip to content

Instantly share code, notes, and snippets.

@muditlambda
Created December 23, 2020 13:12
Show Gist options
  • Save muditlambda/c7b73cfdf317df6b6dea634552d2f6c5 to your computer and use it in GitHub Desktop.
Save muditlambda/c7b73cfdf317df6b6dea634552d2f6c5 to your computer and use it in GitHub Desktop.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment