1.简介

原估计宏哥这里就不对iframe这个知识点做介绍和讲解了,因为前边的窗口切换就为这种网页处理提供了思路,另一个原因就是虽然iframe很强大,但是现在很少有网站用它了。但是还是有小伙伴或者童鞋们私下问这个问题,那么宏哥就单独写一篇关于iframe网页处理的文章。iframe 是web自动化里面一个比较头疼的测试场景,在Selenium中处理 iframe 需要切换来切换去非常麻烦。但是在playwright中,让其变得非常简单,我们在使用中无需切换iframe,直接定位元素即可。

2.iframe是什么

iframe就是我们常用的iframe标签:<iframe>。iframe标签是框架的一种形式,也比较常用到,iframe一般用来包含别的页面,例如我们可以在我们自己的网站页面加载别人网站或者本站其他页面的内容。iframe标签的最大作用就是让页面变得美观。iframe标签的用法有很多,主要区别在于对iframe标签定义的形式不同,例如定义iframe的长宽高。简单的一句话概括就是:iframe 就是HTML 中,用于网页嵌套网页的。 一个网页可以嵌套到另一个网页中,可以嵌套很多层。和俄罗斯套娃差不多吧。

3.iframe语法

page.frame_locator()

locator = page.frame_locator("frame").get_by_text("登录")

说明:使用frame_locator() 定位到iframe上,再在上面使用locator方法定位元素。

可以使用page.frame_locator()或locator.frame_locator()方法创建 FrameLocator 捕获足该 iframe 中检索和定位元素。

使用示例一:

locator = page.frame_locator("my-frame").get_by_text("Submit")
locator.click()

使用frame_locator() 定位到iframe上,然后继续在上面使用locator方法定位元素

iframe 定位器是严格的。这意味着如果有多个元素与给定的选择器匹配,则对 iframe 定位器的所有操作都会抛出异常。

# Throws if there are several frames in DOM:
page.frame_locator('.result-frame').get_by_role('button').click() # Works because we explicitly tell locator to pick the first frame:
page.frame_locator('.result-frame').first.get_by_role('button').click()

以下代码段在带有 id 的 iframe 中定位带有文本“提交”的元素my-frame,例如<iframe id="my-frame">:

locator = frame.frame_locator("#my-iframe").get_by_text("提交")
locator.click()

4.frame定位

匹配第一个

frame_locator().first

匹配最后一个

frame_locator().last

使用index索引

frame_locator().nth(index)

获取全部iframes

page.frames

5.iframe() 定位

根据name属性和url属性匹配

frame = page.frame(name="frame-name")
frame = page.frame(url=r".*domain.*")
frame.fill('#username-input', 'John')

6.page.frame 和 page.frame_locator 区别

page.frame_locator() 返回的对象需要用locator() 方法定位元素,再操作元素
page.frame() 返回的对象可直接使用fill() 、 click() 方法。

7.项目实战

网上找了半天也没有找到这样的例子,以前百度、163的邮箱是这种。最近几年技术升级了,已经不是这种了。不找了索性宏哥自己在本地做一个这样的小demo给小伙伴或者童鞋们来演示一下。

7.1被测的HTML代码

1.准备测试练习index.html,如下:

<!DOCTYPE html>
<html>
<head>
<title>北京-宏哥|iframeTestDemo</title>
<style type="text/css"> .button1 {
background-color: #f44336;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 28px;
margin-bottom: 100px;
text-decoration:none;
color: white;
}
#myAnchor
{
text-decoration:none;
color: white;
}
</style>
</head>
<body style="text-align:center">
<div id="wrapper" style="position: relative;top: 100px;left:0px;">
<button class="button1"><a id="myAnchor" href="https://www.cnblogs.com/du-hong/">北京-宏哥</a></button></br>
<div id="id1">I am a index page's div!</div>
<input type="text" id="maininput" />
<br/>
<iframe id="frameA" frameborder="0" scrolling="no" style="left:857px;position:absolute;" src="iframe.html"></iframe>
</div>
</body>
</html>

2.准备测试练习iframe.html,如下:

<!DOCTYPE html>
<html>
<head>
<title>I am a iframe!</title>
</head>
<body>
<div id="div1">I am iframes div!</div>
<input id="iframeinput"></input>
</body>
</html>

3.页面效果,如下图所示:

8.牛刀小试

8.1代码设计

8.2参考代码

# coding=utf-8

# 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行

# 2.注释:包括记录创建时间,创建人,项目名称。
'''
Created on 2023-07-23
@author: 北京-宏哥 QQ交流群:705269076
公众号:北京宏哥
Project: 《最新出炉》系列初窥篇-Python+Playwright自动化测试-11-playwright操作iframe
''' # 3.导入模块
from playwright.sync_api import Playwright, sync_playwright def run(playwright: Playwright) -> None: browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("C:/Users/DELL/Desktop/test/iframe/index.html")
page.wait_for_timeout(2000)
# 操作非 iframe上的元素
page.locator('[id="maininput"]').fill("I am a index page's div!")
# 操作 iframe 上的元素
frame = page.frame_locator("iframe[id^=frameA]")
# xpath 匹配
frame.locator('[id="iframeinput"]').fill('This is a iframe input!')
page.wait_for_timeout(3000)
# page.pause()
context.close()
browser.close() with sync_playwright() as playwright:
run(playwright)

8.3运行代码

1.运行代码,右键Run'Test',控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作。如下图所示:

9.小结

好了,时间不早了,今天就分享到这里,下一篇宏哥找一个还有iframe的在线网页给小伙伴或者童鞋们实战演示一下。

《最新出炉》系列初窥篇-Python+Playwright自动化测试-11-playwright操作iframe-上篇的更多相关文章

  1. python - 接口自动化测试 - MysqlUtil - 数据库操作封装

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: mysql_util.py @ide: PyCharm C ...

  2. Python+Appium自动化测试(11)-location与size获取元素坐标

    appium做app自动化测试过程中,有时需要获取控件元素的坐标进行滑动操作.appium中提供了location方法获取控件元素左上角的坐标,再通过size方法获取控件元素的宽高,就可以得到控件元素 ...

  3. Flutter 即学即用系列博客——04 Flutter UI 初窥

    前面三篇可以算是一个小小的里程碑. 主要是介绍了 Flutter 环境的搭建.如何创建 Flutter 项目以及如何在旧有 Android 项目引入 Flutter. 这一篇我们来学习下 Flutte ...

  4. Spark系列-初体验(数据准备篇)

    Spark系列-初体验(数据准备篇) Spark系列-核心概念 在Spark体验开始前需要准备环境和数据,环境的准备可以自己按照Spark官方文档安装.笔者选择使用CDH集群安装,可以参考笔者之前的文 ...

  5. Python系列之入门篇——HDFS

    Python系列之入门篇--HDFS 简介 HDFS (Hadoop Distributed File System) Hadoop分布式文件系统,具有高容错性,适合部署在廉价的机器上.Python ...

  6. Python系列之入门篇——MYSQL

    Python系列之入门篇--MYSQL 简介 python提供了两种mysql api, 一是MySQL-python(不支持python3),二是PyMYSQL(支持python2和python3) ...

  7. python爬虫 scrapy2_初窥Scrapy

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  8. WWDC15 Session笔记 - Xcode 7 UI 测试初窥

    https://onevcat.com/2015/09/ui-testing/ WWDC15 Session笔记 - Xcode 7 UI 测试初窥 Unit Test 在 iOS 开发中已经有足够多 ...

  9. Scrapy001-框架初窥

    Scrapy001-框架初窥 @(Spider)[POSTS] 1.Scrapy简介 Scrapy是一个应用于抓取.提取.处理.存储等网站数据的框架(类似Django). 应用: 数据挖掘 信息处理 ...

  10. 初窥Kaggle竞赛

    初窥Kaggle竞赛 原文地址: https://www.dataquest.io/mission/74/getting-started-with-kaggle 1: Kaggle竞赛 我们接下来将要 ...

随机推荐

  1. 2023-04-27:用go语言重写ffmpeg的remuxing.c示例。

    2023-04-27:用go语言重写ffmpeg的remuxing.c示例. 答案2023-04-27: ffmpeg的remuxing.c是一个用于将多媒体文件从一种容器格式转换为另一种容器格式的命 ...

  2. 2021-10-09:杨辉三角。给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行。在「杨辉三角」中,每个数是它左上方和右上方的数的和。力扣118。

    2021-10-09:杨辉三角.给定一个非负整数 numRows,生成「杨辉三角」的前 numRows 行.在「杨辉三角」中,每个数是它左上方和右上方的数的和.力扣118. 福大大 答案2021-10 ...

  3. Windows server 2012 安装ad域

    Windows server 2012 安装ad域   安装ad域(active directory)服务的作用:存储目录数据并管理域之间的通信,包括用户登录处理,身份验证和目录搜索等. 1.使用ad ...

  4. CF1825C LuoTianyi and the Show

    传送门(luogu) 传送门(CF) 前言 我来水题解力 简化题意 \(n\) 个人,\(m\) 个座位,每个人落座的方法有三种: 坐最左边的人的左边,没人的话就做 \(m\) 号座位,若最左边的为 ...

  5. vue项目提示TypeError: e.call is not a function

    最近运行vue项目老是提示TypeError: e.call is not a function 如上一运行到该页面就会提示这个错误,虽然页面功能都没受到影响,但是这个必定会给后期发布后的项目带来极大 ...

  6. 代码随想录算法训练营Day53 动态规划

    代码随想录算法训练营 代码随想录算法训练营Day53 动态规划|●  1143.最长公共子序列 1035.不相交的线 53. 最大子序和 动态规划 1143.最长公共子序列 题目链接:1143.最长公 ...

  7. jquery页面搜索关键词突出显示

    页面搜索关键词突出 // 页面搜索关键词突出 $(function () { $(".list_r").find('span').css({ // 每次搜索开始,先把所有字体颜色恢 ...

  8. Doris(七) -- 修改表、动态和临时分区、join的优化

    修改表 修改表名 -- 1.将名为 table1 的表修改为 table2 ALTER TABLE table1 RENAME table2; -- 示例 ALTER TABLE aggregate_ ...

  9. 【Python&GIS】GDAL栅格转面&计算矢量面积

            GDAL(Geospatial Data Abstraction Library)是一个在X/MIT许可协议下的开源栅格空间数据转换库.它利用抽象数据模型来表达所支持的各种文件格式.它 ...

  10. 全球开源 AI 游戏开发挑战赛,只等你来!

    我们在之前的文章中 预告过 (*划重点,IP 属地法国):7 月初,我们将举办一次与 AI 游戏相关的黑客松活动,这是有史以来的首次开源游戏开发挑战赛,借助人工智能工具释放你的创造力,一起打破游戏开发 ...