软件测试/测试开发丨Selenium 网页frame与多窗口处理

点此获取更多相关资料

本文为霍格沃兹测试开发学社学员学习笔记分享

原文链接:https://ceshiren.com/t/topic/27048

一、多窗口处理.

1.1、多窗口简介

点击某些链接,会重新打开⼀个窗⼜,对于这种情况,想在新页⾯上操作,就

得先切换窗⼜了。

获取窗⼜的唯⼀标识⽤句柄表⽰,所以只需要切换句柄,就可以在多个页⾯灵

活操作了

1.2、多窗口处理流程

  • 先获取当前窗口的句柄driver.current_window_handle
  • 再获取所有的窗口句柄driver.windows_handles
  • 然后判断当前窗口是否为需要操作的窗口,如果不是则,切换到下一个窗口,如果是,则在当前窗口进行操作
python 复制代码
    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而言,则需要进行一些特殊的操作后,才能到定位到元素

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("子节点")
python 复制代码
    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()
相关推荐
测试1998几秒前
如何写出一个完整的测试用例?
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·接口测试
微笑尅乐11 分钟前
三种方法解开——力扣3370.仅含置位位的最小整数
python·算法·leetcode
九章云极AladdinEdu38 分钟前
GitHub Actions for AI:构建企业级模型CI/CD流水线
自动化测试·模型部署·mlops·github actions·深度学习调试·ai ci/cd·企业级流水线
多恩Stone1 小时前
【3DV 进阶-5】3D生成中 Inductive Bias (归纳偏置)的技术路线图
人工智能·python·算法·3d·aigc
闲人编程1 小时前
使用Python进行量化交易入门
开发语言·python·统计分析·lambda·量化·codecapsule
长颈鹿仙女2 小时前
发送 Prompt 指令:判断用户评价是好评还是差评
python·大模型
小兔崽子去哪了3 小时前
PyMySQL 笔记
python
景彡先生3 小时前
Python NumPy广播机制详解:从原理到实战,数组运算的“隐形翅膀”
开发语言·python·numpy
咕白m6253 小时前
Python 查找高亮 Excel 指定数据
python