码迷,mamicode.com
首页 > 编程语言 > 详细

python下彻底解决浏览器多窗口打开与切换问题

时间:2018-07-07 20:44:42      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:time   exec   max   fir   switch   切换   sleep   win   1.5   

# coding=utf-8

from selenium import webdriver
import time
browser=webdriver.Firefox()
#browser.maximize_window() # 窗口最大化

browser.get(‘https://www.baidu.com‘) # 在当前浏览器中访问百度
print("标题:"+browser.title,end="\n当前窗口句柄")
print(browser.current_window_handle) # 输出当前窗口句柄(百度)

# 新开一个窗口,通过执行js来新开一个窗口
js=‘window.open("https://www.sogou.com");‘
browser.execute_script(js)

handles = browser.window_handles # 获取当前窗口句柄集合(列表类型)
print("所有窗口句柄:")
print(handles) # 输出句柄集合

for handle in handles:# 切换窗口(切换到搜狗)
if handle!=browser.current_window_handle:
print(‘switch to ‘,handle)
browser.switch_to_window(handle)
print("标题:"+browser.title,end="\n当前窗口句柄:")
print(browser.current_window_handle) # 输出当前窗口句柄(搜狗)
break


browser.close() #关闭当前窗口(搜狗)
browser.switch_to_window(handles[0]) #切换回百度窗口
print("标题:"+browser.title)
import time
time.sleep(5)
browser.quit()

python下彻底解决浏览器多窗口打开与切换问题

标签:time   exec   max   fir   switch   切换   sleep   win   1.5   

原文地址:https://www.cnblogs.com/dengpeiyou/p/9277878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!