Python selenium2 window Switch instance

Source: Internet
Author: User

Traversing all links in an area of hao123, clicking each link opens a new window, gets the title of the new window, closes the window, switches to the initial window and continues to open the next link

The code is as follows:

#Coding=utf-8 fromSeleniumImportWebdriverImportTimebase_url="http://www.hao123.com"Driver=Webdriver. Firefox () driver.get (base_url) handle_hao123=Driver.current_window_handles= Driver.find_elements_by_css_selector ("Ul.js_bd.site-bd.site-hd0>li>a") forIinchS:i.click () all_handles=Driver.window_handles#Switch to a newly opened window     forHandleinchAll_handles:ifHandle! =Handle_hao123:driver.switch_to.window (handle)PrintDriver.title#Close New Windowdriver.close ()#Switch to initial windowDriver.switch_to.window (handle_hao123)#in order to indicate the final return to the initial window plus a printPrintdriver.title,driver.current_urldriver.close ()

Please correct me in the wrong place.

Python selenium2 window Switch instance

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.