在实际工作中,我们经常会遇到页面切换的情况。就比如当点击了某个功能的按钮后,浏览器出现了新的标签页,需要在这些标签页之间进行切换。要如何通过 selenium 来实现这样的场景呢?这就是我们今天要学习的内容。
selenium 实现浏览器标签页句柄的切换
浏览器标签页本地文件准备
这一段纯粹是因为内容太少,拿来凑字数的...
同样的,这里所使用的是我们本地的 multi.html 文件,点击该文件的 “register”,会跳转至 “www.baidu.com” 。
“multi.html” 文件的前端代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | < html > < head > < meta http-equiv = "content-type" content = "text/html;charset=utf-8" /> < title >Form</ title > < script type = "text/javascript" async = "" src = "jquery.min.js" ></ script > < link href = "bootstrap-combined.min.css" rel = "external nofollow" rel = "stylesheet" /> < script src = "bootstrap.min.js" ></ script > </ head > < body > < h3 >simple login form</ h3 > < form class = "form-horizontal" > < div class = "control-group" > < label class = "control-label" for = "inputEmail" >Email</ label > < div class = "controls" > < input type = "text" id = "inputEmail" placeholder = "Email" name = "email" > </ div > </ div > < div class = "control-group" > < label class = "control-label" for = "inputPassword" >Password</ label > < div class = "controls" > < input type = "password" id = "inputPassword" placeholder = "Password" name = "password" > </ div > </ div > < div class = "control-group" > < div class = "controls" > < label class = "checkbox" > < input type = "checkbox" > Remember me </ label > < button type = "submit" class = "btn" >Sign in</ button > < a href = "https://www.baidu.com" rel = "external nofollow" target = "_blank" >register</ a > </ div > </ div > </ form > </ body > </ html > |
利用 selenium 实现浏览器页面的切换
获取浏览器标签页句柄方法如下:
current_window_handle
获取当前标签页的句柄
window_handles 获取所有标签页的句柄
代码示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # coding:utf-8 from time import sleep from selenium import webdriver driver = webdriver.Chrome() # 启动 Chrome浏览器的 driver driver.maximize_window() # Chrome 浏览器最大化 driver.get( 'file:///Users/workspace/WEB_TEST_HTML/multi.html' ) # 打开本地的 "multi.html" 文件 sleep( 1 ) first_window = driver.current_window_handle # 通过 current_window_handle 属性获取当前标签页的句柄 driver.find_element_by_link_text( 'register' ).click() # 通过 link_text 方法定位到 "register" 元素,执行点击操作(跳转百度) sleep( 2 ) all_windows = driver.window_handles # 通过 window_handles 属性获取当前所有标签页的句柄 for handle in all_windows: # for 循环判断当前句柄是否是 first_window 句柄,如果不是则跳转至该句柄(该句柄为百度页面的句柄) if handle ! = first_window: driver.switch_to.window(handle) print ( "已跳转至 '百度' 标签页的句柄" ) sleep( 1 ) driver.find_element_by_id( 'kw' ).send_keys( '渴望力量的哈士奇' ) # 在百度句柄页面的搜索框输入要百度的内容 "渴望力量的哈士奇" sleep( 1 ) driver.find_element_by_id( 'su' ).click() # 点击 "百度一下" 按钮 sleep( 2 ) driver.switch_to.window(first_window) # 跳转至 "multi.html" 页面的句柄 email_element = driver.find_element_by_xpath( '//*[@id="inputEmail"]' ) # 通过 xpath 定位 Email 输入框。 email_element.send_keys( 'username' ) # Email 输入框输入 "username" sleep( 1 ) email_element.clear() # 清除 Email 输入框内容 sleep( 1 ) email_element.send_keys( 'admin' ) # Email 输入框输入 "admin" driver.find_element_by_xpath( '//*[@id="inputPassword"]' ).send_keys( '123456' ) # Password 输入框输入 "123456" sleep( 1 ) driver.find_element_by_xpath( '/html/body/form/div[3]/div/button' ).click() sleep( 1 ) driver.quit() |
运行结果如下:
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:485187702【暗号:csdn11】
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】