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