Selenium —— 网页frame与多窗口处理!

一、多窗口处理.

1.1、多窗口简介

点击某些链接,会重新打开⼀个窗⼜,对于这种情况,想在新页⾯上操作,就
得先切换窗⼜了。
获取窗⼜的唯⼀标识⽤句柄表⽰,所以只需要切换句柄,就可以在多个页⾯灵
活操作了

1.2、多窗口处理流程

  • 先获取当前窗口的句柄driver.current_window_handle

  • 再获取所有的窗口句柄driver.windows_handles

  • 然后判断当前窗口是否为需要操作的窗口,如果不是则,切换到下一个窗口,如果是,则在当前窗口进行操作

    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)

二、多网页frame处理

2.1、frame简介

在web自动化中,如果一个元素始终无法定位,那么很有可能是frame中

  • 什么是frame呢?

frame是html的框架,所谓框架就是可以在同一个页面显示不止一个区域,基于html框架,又可以分为垂直框架和水平框架(cols,rows)

  • frame分类
    • frame标签分为frameset,ifame、frame三种

    • frameset和普通的标签一样,不会影响正常的元素定位,可以使用index、id、name、webelement等方式定位到frame

    • frame、iframe相当于selenium而言,则需要进行一些特殊的操作后,才能到定位到元素

      现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
      如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
      可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
      分享他们的经验,还会分享很多直播讲座和技术沙龙
      可以免费学习!划重点!开源的!!!
      qq群号:110685036

2.2、多frame切换

  • frame存在两种
    • 一种嵌套的
    • 一种未嵌套的
  • 切换frame
    • driver.swich_to.frame():根据元素id、index切换frame
    • driver.switch_to.default_content():切换到默认的frame
    • deiver.switch_to.parent_frame():切换到父级frame

未嵌套的frame

  • driver.switch_to.frame('frame的id'):有id时优先使用id
  • driver.switch_to.frame('frame-index'):没有id的时间根据索引来处理,索引从0开始

嵌套的frame

  • 对于嵌套的frame,则先进入到frame的父节点,再进到子节点,然后可以就可以子节点中的元素对象进行操作了

  • driver.switch_to.frame("父节点")

  • driver.switch_to.frame("子节点")

    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今天的分享就到此结束了,对你有所帮助的话留下小心心哈!

相关推荐
程序员龙叔13 天前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
goldenrolan13 天前
A公司物料替代测试系统 v1.7:从需求到 exe/apk 的 AI 辅助全链路实践
android·自动化测试·软件测试·python·ai
Raina测试13 天前
欢迎来到《Raina 测试学习指南》博客✨ | Raina测试学习指南
软件测试·ai测试
程序员小远13 天前
自动化测试基础知识总结
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
goldenrolan13 天前
学习型红外控制系统稳定性挂测工装专项总结
软件测试·python·stm32·嵌入式·红外
LT101579744413 天前
2026年Web自动化测试工具选型指南:多浏览器兼容解决方案
前端·测试工具·自动化
糖果店的幽灵13 天前
软件测试接口测试从入门到精通:接口测试CI_CD集成
软件测试·ci/cd·接口测试
程序员三藏14 天前
Web自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
深蓝电商API14 天前
Selenium 5.0 全新架构解析:值得升级吗?
爬虫·selenium