首页 > 编程语言 >《最新出炉》系列初窥篇-Python+Playwright自动化测试-11-playwright操作iframe-上篇

《最新出炉》系列初窥篇-Python+Playwright自动化测试-11-playwright操作iframe-上篇

时间:2023-08-11 14:57:45浏览次数:36  
标签:11 Playwright Python frame locator 宏哥 playwright iframe page

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

标签:11,Playwright,Python,frame,locator,宏哥,playwright,iframe,page
From: https://www.cnblogs.com/du-hong/p/17563277.html

相关文章

  • VS2019 C++ 调用python函数/类对象的方法
    1.环境配置VS工程配置要和python一致,安装的python如果是64位的,工程配置也要选成64位的在工程配置中添加包含目录和库目录,添加python环境目录里的include和libs文件夹路径。想要运行的keras-yolo3是在Anaconda中配置的环境,所以相应的文件夹路径可以在Anaconda的环境文件中......
  • 「Log」2023.8.11 小记
    间幕\(1\)从今天开始记小记。七点到校了,先小摆一会,然后整理博客。听MITiS的电音,开始写题。\(\color{blueviolet}{P1829\[国家集训队]\Crash的数字表格\/\JZPTAB}\)莫反练习题,式子并不难推,两个整除分块解决。八点整打完,开始调。忘记初始化了。筛质数pri[++pcnt]=tr......
  • linux python版本切换alternatives
    查看发现linux的python3命令指向alternativesalternatives是Linux系列操作系统的一个内置命令,即使最小化安装也有该命令,它的主要作用就是版本控制切换,比如,你的系统内有多个Python版本,Python3.8,Python2.7.5,Python3.6,。[root@santiagod-andibleopenstack-ansible]#find/usr/b......
  • Python打印类的属性
    一、使用__dict__打印类的属性classPerson:def__init__(self,name,age):self.name=nameself.age=ageperson=Person("Tom",18)print(person.__dict__)使用__dict__方法可以直接打印出类的属性及其对应的值。上述代码中,我们首先定义了一个Pe......
  • FTData063468_000001升级脚本出错,错误信息:SQL 脚本: 18.000.000.0048 DATA_DSTR_EAP_M
    一、问题:cjt15.0版本升级到18.0提示SQL脚本:18.000.000.0048DATA_DSTR_EAP_Mix_NL-11001出错:已在列上绑定了DEFAULT023-08-1019:46:39开始升级....2023-08-1019:46:39正在校验系统信息,请稍候...2023-08-1019:46:39[(000001)****]:开始升级2023-08-1019:46:39[(......
  • MySQL 1130错误原因及解决方案
    错误:ERROR1130:Host‘http://xxx.xxx.xxx.xxx’isnotallowedtoconnecttothisMySQLserve错误1130:主机xxx.xxx.xxx.xxx”不允许连接到thismysql服务原因分析被连接的数据不允许使用主机http://xxx.xxx.xxx.xxx访问,系统数据库mysql中user表中的host是localhost,只允许......
  • [数据分析与可视化] Python绘制数据地图5-MovingPandas绘图实例
    MovingPandas是一个基于Python和GeoPandas的开源地理时空数据处理库,用于处理移动物体的轨迹数据。关于MovingPandas的使用见文章:MovingPandas入门指北,本文主要介绍三个MovingPandas的绘图实例。MovingPandas官方仓库地址为:movingpandas。MovingPandas官方示例代码仓库地址为:movin......
  • python在抛出错误raise时,如何将相关信息一同携带出来(抛出自定义对象)?
    示例代码示例classMyClass(Exception):def__init__(self,info):self.info=infodefmy_method(self):print('MyClass.my_methodprintself.info:',self.info)try:raiseMyClass('错误信息')exceptMyClassase:......
  • C/C++住院病人管理系统[2023-08-11]
    C/C++住院病人管理系统[2023-08-11]22、住院病人管理系统(难度等级8)使用C或C++,选择一种计算机编程软件和数据库管理系统来实现一个住院病人管理系统。系统需要实现的功能如下:(1)添加、删除和修改病人信息:向系统中添加、删除和修改仓库信息,信息包括(住院号、姓名、年龄、住院时间、......
  • 时序分析:Python 中的 ARIMA 模型
    推荐:使用NSDT场景编辑器快速助你搭建可二次编辑的3D应用场景什么是ARIMA模型?ARIMA模型是用于分析和预测时间序列数据的统计模型。ARIMA方法明确迎合了时间序列中的标准结构,为制作熟练的时间序列预测提供了一种简单而强大的方法。ARIMA代表自回归积分移动平均线。它结合了三......