码迷,mamicode.com
首页 > 其他好文 > 详细

selenium 多窗口切换

时间:2019-01-28 12:07:19      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:end   搜索   一个   name   port   windows   account   循环   search   

#coding=utf-8
#多窗口:如点了某个链接后,会再多打开一个新窗口,即新标签页
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.get("http://www.baidu.com")

#获得当前窗口句柄
search_windows = driver.current_window_handle
driver.find_element_by_link_text(‘登录‘).click()
driver.find_element_by_link_text(‘立即注册‘).click()
#获得当前打开所有窗口的句柄
all_handles = driver.window_handles

#进入注册窗口
for handle in all_handles:
if handle !=search_windows:
#如果当前窗口不是百度首页的句柄,则切换到注册句柄
driver.switch_to_window(handle)
print (‘now register window!‘)
zhuce_windows = driver.current_window_handle
driver.find_element_by_name("account").send_keys(‘username‘)
driver.find_element_by_name("password").send_keys(‘password‘)


#返回搜索窗口
for handle in all_handles:
if handle == search_windows:
driver.switch_to_window(handle)
print ("now resarch window")

#如果当前打开的不止两个窗口,则需要在每个窗口打开的时候,获取一次句柄,要切换到任何一个窗口,只需要再switch一下
#如这个是获取注册页的句框zhuce_windws = driver.current_window_handle
#切换时只要driver.switch_to_window(zhuce_windows) ,或者使用for +and进行循环

‘‘‘
for handle in all_handles:
if handle != search_windows and handle !=zhuce_windows
driver.switch_to_window(handle)
print ("now is the thirds window")
‘‘‘

selenium 多窗口切换

标签:end   搜索   一个   name   port   windows   account   循环   search   

原文地址:https://www.cnblogs.com/bzdmz/p/10329163.html

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