Scenario:
During the operation page sometimes click a link will pop up a new window, this time you need to switch to a new host open window to operate. Providing WebDriver switch_to.window () method, the switching can be implemented directly in the different windows.
Home to Baidu and Baidu registration page, for example, in a direct switch shown in two windows
code show as below:
1 # guiding packet 2 from Selenium Import the webdriver . 3 from Time Import SLEEP . 4 # custom browser handle, open Baidu URLs . 5 Driver = webdriver.Chrome () . 6 driver.implicitly_wait (10 ) . 7 URL = " http://www.baidu .com " . 8 driver.get (URL) . 9 # window is maximized 10 driver.maximize_window () . 11 # acquires Baidu search window handle 12 is seach_windows = driver.current_window_handle 13 is # print Baidu search window handle 14 Print (seach_windows) 15 # upper right corner click on the "Login" button 16 driver.find_element_by_link_text ( " login " ) .click () 17 # Click the "Register Now" button in the pop in 18 driver.find_element_by_xpath ( ' // * [@ = ID "Login-Passport-POP-Dialog"] / div / div / div / div [. 4] / a ' ) .click () . 19 # Gets all currently open window handle 20 is all_handles = driver.window_handles 21 is # into the register window 22 for newhandle in all_handles: 23 IF newhandle =! seach_windows: 24- driver.switch_to.window (newhandle) 25 Print ( ' ! now the Register window ' ) 26 SLEEP (1 ) 27 # to enter a user name: username12334 28 driver.find_element_by_id ( " TANGRAM__PSP_4__userName " ) .send_keys ( " username12334 " ) 29 # to enter the phone number: 18,877,776,666 30 driver.find_element_by_id ( " TANGRAM__PSP_4__phone " ) .send_keys ( " 18,877,776,666 " ) 31 # password: password 32 driver.find_element_by_id ( " TANGRAM__PSP_4__password " ) .send_keys ( " password " ) 33 is # Click for voice verification code 34 is driver.find_element_by_id ( " TANGRAM__PSP_4__verifyCodeSend " ) .click () 35 # Enter the code: 123456 36 driver.find_element_by_id ( " TANGRAM__PSP_4__verifyCode " ) .send_keys ( " 123456 " ) 37 # check (read and accept the" Baidu user agreement "and" Baidu Privacy statement ") 38 driver.find_element_by_id ( "TANGRAM__PSP_4__isAgree " ) .click () 39 # click on the" Register "button 40 driver.find_element_by_id ( " TANGRAM__PSP_4__submit " ) .click () 41 # returned to Baidu search window 42 for newhandle in all_handles: 43 IF newhandle == seach_windows: 44 Driver. switch_to.window (newhandle) 45 Print ( ' now Seach window! ' ) 46 is SLEEP (. 1 ) 47 # Close login / register immediate pop 48 driver.find_element_by_id ( " TANGRAM__PSP_4__closeBtn " ) .click () 49 # Click Baidu input box "Selenium the webdriver" 50 driver.find_element_by_xpath ( ' // INPUT [@ ID = "kW"] ' ) .send_keys ( " Selenium the webdriver " ) 51 # click the "Baidu it" button 52 driver.find_element_by_xpath ( ' // the INPUT [@ the above mentioned id = "su"] ' ) .click () 53 SLEEP (2 ) 54 # Close all windows and exit the browser, the end of this script task 55 driver.quit ()
Script execution: First, open the Baidu home page, get the handle of the current window by current_window_handle, and assigned to the variable seach_windows then open the login window, click the "Register Now" on the login pop, thus opening a new registration window. Gets a handle of all windows currently open by window_handles, and assigned to the variable all_handles
The first for loop iterates all_handles, if not equal to newhandle seach_windows, then it must be registered window because during script execution only open two windows. Therefore, the register operated by switch_to.window () to switch to the registration page. Similarly for the second loop, but this time determines if newhandle equal seach_windows, then switch to Baidu search page and search operations.
to sum up:
current_window_handle: get a handle to the current window
window_handles: Returns the handle of all windows to the current session
switch_to.window (): for switching to the appropriate window, similar to switch_to.frame (), the former is used to switch between different window, which is used to switch between different form.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
talk is cheap,show me the code.