打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python selenium切换窗口(获取句柄信息)
userphoto

2023.01.03 浙江

关注
current_window_handle:获得当前窗口句柄;

window_handles:返回所有窗口的句柄到当前会话;

switch_to.window(suoyou[0])


===========================================================================================================

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox() # 指定和打开浏览器

driver.get('http://home.baidu.com/')

print('=============================================================1:')
print(driver.current_window_handle) # 用于获取当前窗口句柄
print(driver.current_url)

driver.find_element_by_link_text('加入我们').click()

sleep(10)

print('=================================================================2:')

print(driver.current_window_handle) #用于获取当前窗口句柄

print(driver.current_url)


print('=================================================================3:')

suoyou = driver.window_handles # 用于获取所有窗口句柄

print(suoyou) # 打印所有的句柄

#--------------------------------------------------------------------------

sleep(5)
driver.switch_to.window(suoyou[0]) #切换到第一个句柄
sleep(5)

driver.switch_to.window(suoyou[1]) #切换到第二个句柄
sleep(5)

driver.close() #关闭新窗口
sleep(5)

driver.quit() #关闭浏览器 或者:driver.close()


===============================================================================================================

================================================================================================================

执行结果:

=============================================================1:
17
http://home.baidu.com/
=================================================================2:
17
http://home.baidu.com/
=================================================================3:
['17', '6442450947']


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【WebDriver API】python之selenium多窗口切换
Selenium2+python自动化13
关于Python+selenium 定位浏览器弹窗元素
《selenium2 python 自动化测试实战》(9)——切换窗口
UI自动化测试之selenium工具(浏览器窗口的切换)
selenium+python显式等待和隐式等待
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服