Selenium get the multi-window handle and switch to the original window handle (three windows)

Source: Internet
Author: User

There are many online selenium based on Python to get two window handle and switch, this paper implemented with Python+selenium to get a multi-window handle and switch to the original window handle (three windows), and under each window to do a search or translation, and then.

The code is as follows:

1 #Coding=utf-82  fromSeleniumImportWebdriver3 Import Time4 5Driver =Webdriver. Chrome ()6Driver.maximize_window ()#window Maximization7 8Driver.get ('https://www.baidu.com')#visit Baidu in the current browser9Time.sleep (2)Ten #print (driver.current_window_handle) # Output Current Window handle (Baidu) OneFrist_handle =Driver.current_window_handle A  - #Open a new window, by executing JS to open a new window, Access Sogou -js='window.open ("https://www.sogou.com");' the driver.execute_script (JS) -  - #Open a new window, by executing JS to open a new window, Access Youdao -js='window.open ("http://www.youdao.com/");' + driver.execute_script (JS) -  +Handles = Driver.window_handles#gets the current Window handle collection (list type) A Print(handles)#Output Handle Collection at  -  forHandleinchHandles#Toggle Window (switch to Youdao) -     ifHandle! =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 inDriver.find_element_by_css_selector ("Button"). Click () -Driver.get_screenshot_as_file ("D:\windows\\youdao.jpg")#Customizable save location (D:\windows) and picture naming (youdao.jpg) toTime.sleep (5) +          Break -Driver.close ()#Close the current window (Youdao) the  *  forHandleinchHandles#Switch window (switch to Sogou) $     ifHandle! =Frist_handle:Panax NotoginsengDriver.switch_to_window (Handles[-1])#at this point only two handles are left, taking the last one -         #print (driver.current_window_handle) # Output Current Window handle (Sogou) theDRIVER.FIND_ELEMENT_BY_ID ("Query"). Send_keys ("Selenium")#Sogou Search Selenium +DRIVER.FIND_ELEMENT_BY_ID ("STB"). Click () ATime.sleep (2)#wait 2s in order to intercept the complete search result graph theDriver.get_screenshot_as_file ("D:\windows\\sougou.jpg")#Customizable save location and picture naming +Time.sleep (5) -          Break $Driver.close ()#Close the current window (Sogou) $  -Driver.switch_to_window (Frist_handle)#switch back to Baidu window -DRIVER.FIND_ELEMENT_BY_ID ("kw"). Send_keys ("Selenium")#Baidu Search Selenium theDRIVER.FIND_ELEMENT_BY_ID ("su"). Click () -Time.sleep (2)#wait 2s in order to intercept the complete search result graphWuyiDriver.get_screenshot_as_file ("D:\windows\\baidu.jpg")#Customizable save location and picture naming theTime.sleep (5) -Driver.quit ()#Exit Browser

As follows:

Selenium get the multi-window handle and switch to the original window handle (three windows)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.