
点击某些链接,会重新打开⼀个窗⼜,对于这种情况,想在新页⾯上操作,就
得先切换窗⼜了。
获取窗⼜的唯⼀标识⽤句柄表⽰,所以只需要切换句柄,就可以在多个页⾯灵
活操作了
- def test_switch_window(self):
- """窗口切换操作"""
- # 1、打开百度
- self.driver.get("https://www.baidu.com")
- print(self.driver.title, self.driver.current_window_handle)
- # 2、打开搜狗
- self.driver.switch_to.new_window()
- self.driver.get("https://www.sougou.com")
- print(self.driver.title, self.driver.current_window_handle)
- # 3、打开hao360
- self.driver.switch_to.new_window()
- self.driver.get("https://hao.360.com/")
- print(self.driver.title, self.driver.current_window_handle)
- # 4、打开测试人
- self.driver.switch_to.new_window()
- self.driver.get("https://ceshiren.com")
- print(self.driver.title, self.driver.current_window_handle)
- handles = self.driver.window_handles
- print(handles)
- self.driver.switch_to.window(handles[0])
- self.driver.switch_to.window(handles[-1])
- print(self.driver.title)
在web自动化中,如果一个元素始终无法定位,那么很有可能是frame中
frame是html的框架,所谓框架就是可以在同一个页面显示不止一个区域,基于html框架,又可以分为垂直框架和水平框架(cols,rows)
- 现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
- 如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
- 可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
- 分享他们的经验,还会分享很多直播讲座和技术沙龙
- 可以免费学习!划重点!开源的!!!
- qq群号:110685036

- def test_switch_frame(self):
- # ❖ 打开包含frame的web页⾯ https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable
- # ❖ 打印’请拖拽我’元素的⽂本
- # ❖ 打印’点击运⾏’元素的⽂
- self.driver.get("https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable")
- self.driver.switch_to.frame("iframeResult")
- ele01 = self.driver.find_element(By.ID, "draggable")
- print(ele01.text)
- ele02 = self.driver.find_element(By.ID, "droppable")
- print(ele02.text)
- self.action.drag_and_drop(ele01, ele02).perform()
- time.sleep(3)
- self.driver.switch_to.alert.accept()
- # self.driver.switch_to.default()
- # self.driver.switch_to.parent_frame()
END今天的分享就到此结束了,对你有所帮助的话留下小心心哈!