#Encoding=utf-8ImportUnitTest fromSeleniumImportWebdriverImport TimeImportWin32API, Win32convk_code={'Ctrl': 0x11,'T': 0x54,'Tab': 0x09}#Keyboard Key PressdefKeyDown (keyName): Win32api.keybd_event (Vk_code[keyname], 0, 0, 0)#keyboard keys lift updefkeyUp (keyName): Win32api.keybd_event (Vk_code[keyname], 0, Win32con. Keyeventf_keyup, 0)#encapsulated Key-methoddefSimulatekey (Firstkey, Secondkey): KeyDown (Firstkey) KeyDown (Secondkey) keyUp (Secondkey) keyUp (Firstkey)
classTestdemo (unittest. TestCase):defsetUp (self):#get the browser driver instanceSelf.driver = Webdriver. Ie (Executable_path ="E:\\iedriverserver") #self.driver = Webdriver. Firefox (Executable_path = "E:\\geckodriver") defTest_newtab (self):#wait 3 seconds for the browser to start to completeTime.sleep (3) #use a For loop, and then open two new tab pages forIinchRange (2): Simulatekey ("Ctrl","T") #switch the current page to the default page by using CTRL + TAB key combinations. #which is the first tab to open.Simulatekey ("Ctrl","Tab") #Visit Sogou homepageSelf.driver.get ("http://sogou.com") self.driver.find_element_by_id ("Query"). Send_keys (U"the road to glory") self.driver.find_element_by_id ("STB"). Click () time.sleep (3) #self.asserttrue (U "Josh Lucas" in Self.driver.page_source) #Get all open window handlesAll_handles =Self.driver.window_handlesPrintLen (all_handles) forHandleinchAll_handles:#switch the current window handle to a second tabSelf.driver.switch_to.window (handle)PrintSelf.driver.titleifU"Input Method" not inchSelf.driver.page_source:self.driver.get ("http://www.baidu.com") self.driver.find_element_by_id ("kw"). Send_keys (U"Webdriver Practical Treasure") self.driver.find_element_by_id ("su"). Click () time.sleep (3) self.asserttrue (U"Practical Treasure" inchSelf.driver.page_source)elif(U"Input Method" not inchSelf.driver.page_source) and("Webdriver" not inchself.driver.page_source):#switch the handle of the current window to the Third tab pageSelf.driver.get ("http://www.iciba.com") Time.sleep (3) self.asserttrue (U"Search Words" inchSelf.driver.page_source)defTearDown (self):#Exit Browserself.driver.quit ()if __name__=='__main__': Unittest.main ()
Based on Firefox:
#Encoding=utf-8ImportUnitTest fromSeleniumImportWebdriverImport TimeImportWin32API, Win32convk_code={'Ctrl': 0x11,'T': 0x54,'Tab': 0x09}#Keyboard Key PressdefKeyDown (keyName): Win32api.keybd_event (Vk_code[keyname], 0, 0, 0)#keyboard keys lift updefkeyUp (keyName): Win32api.keybd_event (Vk_code[keyname], 0, Win32con. Keyeventf_keyup, 0)#encapsulated Key-methoddefSimulatekey (Firstkey, Secondkey): KeyDown (Firstkey) KeyDown (Secondkey) keyUp (Secondkey) keyUp (Firstkey)
classTestdemo (unittest. TestCase):defsetUp (self):#get the browser driver instance #self.driver = Webdriver. Ie (Executable_path = "E:\\iedriverserver")Self.driver = Webdriver. Firefox (Executable_path ="E:\\geckodriver") defTest_newtab (self):#wait 3 seconds for the browser to start to completeTime.sleep (3) #use a For loop, and then open two new tab pages forIinchRange (2): Simulatekey ("Ctrl","T") #switch the current page to the default page by using CTRL + TAB key combinations. #which is the first tab to open.Simulatekey ("Ctrl","Tab") #Visit Sogou homepageSelf.driver.get ("http://sogou.com") self.driver.find_element_by_id ("Query"). Send_keys (U"the road to glory") self.driver.find_element_by_id ("STB"). Click () time.sleep (3) #self.asserttrue (U "Josh Lucas" in Self.driver.page_source) #Get all open window handlesAll_handles =Self.driver.window_handlesPrintLen (all_handles)#switch the current window handle to a second tabSelf.driver.switch_to.window (all_handles[1]) Self.driver.get ("http://www.baidu.com") self.driver.find_element_by_id ("kw"). Send_keys (U"Webdriver Practical Treasure") self.driver.find_element_by_id ("su"). Click () time.sleep (3) self.asserttrue (U"Wu Xiaohua" inchSelf.driver.page_source)#switch the handle of the current window to the Third tab pageSelf.driver.switch_to.window (all_handles[2]) Self.driver.get ("http://www.baidu.com") self.driver.find_element_by_id ("kw"). Send_keys ("Selenium") self.driver.find_element_by_id ("su"). Click () time.sleep (3) Self.asserttrue ("www.seleniumhq.org" inchSelf.driver.page_source)defTearDown (self):#Exit Browserself.driver.quit ()if __name__=='__main__': Unittest.main ()
Webdriver Advanced App-New tab in Browser (tab)