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


    1.简介

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

    2.iframe是什么

    iframe就是我们常用的iframe标签:

    复制代码

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

    复制代码
    
    
    
        I am a iframe!
    
    
        
    I am iframes div!
    复制代码

    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的在线网页给小伙伴或者童鞋们实战演示一下。

  • 相关阅读:
    Java现在还适合入门吗?
    论文解读(PairNorm)《PairNorm: Tackling Oversmoothing in GNNs》
    Golang对接Ldap(保姆级教程:概念&搭建&实战)
    Java多线程案例
    curl快速学习指南:从新手到专家
    C++ 多线程
    LaTeX中的积分符号
    使用Process Explorer查看线程的函数调用堆栈去排查程序高CPU占用问题
    盛唐硬币的另一面:山水田园诗
    两个坑:Integer对象比较,MySql中in()、not in()为空
  • 原文地址:https://www.cnblogs.com/du-hong/p/17563277.html