from selenium import webdriver
browser=webdriver.Firefox()
browser.maximize_window()
browser.get('https://www.baidu.com')
js='window.open("https://www.sogou.com");'
browser.execute_script(js)
print browser.current_window_handle
handles = browser.window_handles
print handles
for handle in handles:
if handle!=browser.current_window_handle:
print 'switch to ',handle
browser.switch_to_window(handle)
print browser.current_window_handle
break
browser.close()
browser.switch_to_window(handles[0])
import time
time.sleep(10)
browser.quit()