Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Selenium gets multiple window handles and switches to the original window handle one by one (three windows)

Shulou Source: shulou.com Published: 2022-06-03 05:09:38 09月23日 Update

On the Internet, there are many selenium to get two window handles and switch based on python. This paper uses python+selenium to obtain multi-window handles and switch to the original window handles (three windows), and do a search or translation under each window, and then take a screenshot.

The code is as follows:

# coding=utf-8from selenium import webdriverimport timedriver = webdriver.Chrome () driver.maximize_window () # window maximization driver.get ('https://www.baidu.com') # visits Baidu time.sleep (2) # print (driver.current_window_handle) # output current window handle (Baidu) frist_handle = driver.current_window_handle# opens a new window in the current browser Open a new window by executing js and visit Sogou js='window.open ("https://www.sogou.com");" 'driver.execute_script (js) # Open a new window, open a new window by executing js, and visit youdao js='window.open ("http://www.youdao.com/");" 'driver.execute_script (js) handles = driver.window_handles # get current window handle collection (list type) print (handles) # output handle collection for handle in handles:# switch window (switch to youdao) if handle! = frist_handle: driver.switch_to_window (handle) # print (driver.current_window_handle) # output current window handle (youdao) Driver.find_element_by_id ("translateContent"). Send_keys ("selenium") # youdao translation selenium driver.find_element_by_css_selector ("button"). Click () # driver.find_element_by_css_selector ("[data-rlog='search-popup-close-win']"). Click () driver.find_element_by_css_selector ("[class='close js_close']] "). Click () # close pop-up window driver.get_screenshot_as_file (" D:\ windows\\ youdao.jpg ") # screenshot to customize the saved location (D:\ windows) and picture naming (youdao.jpg) time.sleep (5) breakdriver.close () # close the current window (youdao) for handle in handles:# switch window (switch to Sogou ) if handle! = frist_handle: driver.switch_to_window (handles [- 1]) # now there are only two handles left Take the last # print (driver.current_window_handle) # to output the current window handle (Sogou) driver.find_element_by_id ("query"). Send_keys ("selenium") # Sogou search selenium driver.find_element_by_id ("stb"). Click () time.sleep (2) # wait 2s in order to intercept the complete search result graph driver .get _ screenshot_as_file ("D:\ windows\\ sougou.jpg") # screenshot can customize where to save the screenshot and name the image time.sleep (5) breakdriver.close () # close the current window (Sogou) # driver.switch_to_window (frist_handle) # switch back to Baidu window driver.switch_to_window (handles [0]) # switch back to Baidu window driver .find _ element_by_id ("kw") .send_keys ("selenium") # Baidu search seleniumdriver.find_element_by_id ("su") .click () time.sleep (2) # wait 2s in order to capture the complete search result image driver.get_screenshot_as_file ("D:\ windows\\ baidu.jpg") # screenshot can customize the preservation location after screenshot and the image name time.sleep (5) driver.quit () # exit the browser

The screenshot is as follows:

Tags: Handle switch Screenshot Sogou search New Open output location Picture two search results browser results browsing three Max Code Type Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Huawei vpn Shulou Information NVidia