There are many online selenium based python is acquired window handle and two switching implemented herein to obtain multiple python + selenium and 11 to switch to the window handle of the original window handle (three windows), and performed a search at each window or translation and screenshots.
code show as below:
. 1 # Coding. 8 = UTF- 2 from Selenium Import the webdriver . 3 Import Time . 4 . 5 Driver = webdriver.Chrome () . 6 driver.maximize_window () # window is maximized . 7 . 8 driver.get ( ' https://www.baidu.com ' ) # access Baidu in the current browser . 9 the time.sleep (2 ) 10 # Print (driver.current_window_handle) # outputs the current window handle (Baidu) . 11 frist_handle = driver.current_window_handle 12 is 13 is #Open a new window, by performing js to open a new window to access search dogs 14 js = ' the window.open ( "https://www.sogou.com"); ' 15 driver.execute_script (js) 16 . 17 # Zaixin opening a window, by performing js to open a new window, to access the proper way 18 is js = ' the window.open ( "http://www.youdao.com/"); ' . 19 driver.execute_script (js) 20 is 21 is Handles = driver.window_handles # get the current window handle set (list type) 22 is Print (handles) # output handle set 23 is 24 for handle in handles: # switching windows (switch to the Word) 25 IF handle =! frist_handle: 26 driver.switch_to_window(handle) 27 #print(driver.current_window_handle) # 输出当前窗口句柄(有道) 28 driver.find_element_by_id("translateContent").send_keys("selenium") #有道翻译selenium 29 driver.find_element_by_css_selector("button").click() 30 #driver.find_element_by_css_selector("[data-rlog='search-popup-close-win']").click() 31 driver.find_element_by_css_selector("[class='close js_close']") .click () # Close popup 32 driver.get_screenshot_as_file ( " D: \ Windows \\ youdao.jpg " ) # Screenshot self storage location (D after the definition of the screenshot: \ windows) and picture naming (youdao.jpg) 33 is the time.sleep (. 5 ) 34 is BREAK 35 driver.close () # Close the current window (Wealth) 36 37 [ for handle in Handles: # switching windows (switch to search dogs) 38 is IF ! handle = frist_handle: 39 driver.switch_to_window ( handles [-1]) # time only two handles, take the last 40 #print (driver.current_window_handle) # outputs the current window handle (search dog) 41 is driver.find_element_by_id ( " Query " ) .send_keys ( " Selenium " ) # Sogou search Selenium 42 is driver.find_element_by_id ( " STB " ) .click () 43 is Time. SLEEP (2) # wait 2s to complete the cross-sectional view of search results 44 driver.get_screenshot_as_file ( " D: \ Windows \\ sougou.jpg " ) # screenshot save location can be self-definition screenshots and pictures after naming 45 the time.sleep (5 ) 46 is BREAK 47driver.close () # Close the current window (search dog) 48 49 # driver.switch_to_window (frist_handle) # Baidu switch back window 50 driver.switch_to_window (Handles [0]) # switch back Baidu window 51 is driver.find_element_by_id ( " kW " ) .send_keys ( " the Selenium " ) # Baidu search for the Selenium 52 driver.find_element_by_id ( " su " ) .click () 53 the time.sleep (2) # wait 2s to complete the cross-sectional view of search results 54 driver.get_screenshot_as_file ( " D: \ Windows baidu.jpg \\ " ) #Screenshot can be custom screenshot save location and picture naming 55 the time.sleep (5 ) 56 driver.quit () # exit the browser
Screenshot below:
----------------------------------------------------------------------------------------
talk is cheap , show me the code.