python selenium打开新窗口,多窗口切换


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()

 

打开多个浏览器句柄和标签页的对应关系:

标签页顺序(按照打开顺序):1 2 3 4 5

对应的句柄   :0 4 3 2 1


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM