playwright使用心得
注意:我这里使用虚拟环境
一、playwright官网(非常详细)
playwright官方文档(python)
二、playwright优势
使用总结:开箱即用
支持录制,脚本清晰,方便维护1.支持同步异步俩种方式
2.不需要为每个浏览器下载driver
3.相对比selenium多了一层context
4.支持无头浏览器
5.运行脚本的时候支持打开开发者工具devtools
6.可以使用传统的定位方式,也可以使用playwright自己的方式或者自定义方式
7.比selenium启动和执行速度更快
8.selenium底层是http单项通讯,playwright是基于websocket双向通信
9.playwright是自动等待,无需做太多处理
10.灵活便捷,多页面切换,无需iframe
11.不太懂的方法或者类可以通过录制去了解使用过程
12.回放效率高,回归效率高
13.底层高可用性和稳定性,我觉得都不需要二次封装
14.支持python、java、js、ts、C# 语言
三、录制脚本
1.当我不知道有些方法的使用过程的时候,我会录制脚本查看脚本内容
from playwright.sync_api import Playwright, sync_playwright, expectdef run(playwright: Playwright) -> None:browser = playwright.chromium.launch(headless=False)context = browser.new_context()# Open new pagepage = context.new_page()# Go to https://www.gaojs.com.cn/page.goto("https://www.gaojs.com.cn/")# Click text=关于作者 >> nth=0page.locator("text=关于作者").first.click()page.wait_for_url("https://www.gaojs.com.cn/s/about")# Click text=图床 >> nth=0with page.expect_popup() as popup_info:page.locator("text=图床").first.click()page1 = popup_info.valuepage.wait_for_url("http://image.gaojs.com.cn/explore/trending")# Click text=探索page1.locator("text=探索").click()# Click text=最近page1.locator("text=最近").click()page1.wait_for_url("http://image.gaojs.com.cn/explore/recent")# Click a:has-text("Music") >> nth=0page.locator("a:has-text(\"Music\")").first.click()page.wait_for_url("https://www.gaojs.com.cn/s/music")# Click svg >> nth=0page.locator("svg").first.click()# Close pagepage1.close()# Close pagepage.close()# ---------------------context.close()browser.close()with sync_playwright() as playwright:run(playwright)
2.点击跳转新页面(由录制脚本1内容可知道)
# Click text=图床 >> nth=0with page.expect_popup() as popup_info:page.locator("text=图床").first.click()page1 = popup_info.value# 这里需要改一下,录制出来是page,其实是page1page1.wait_for_url("http://image.gaojs.com.cn/explore/trending")# Click text=探索page1.locator("text=探索").click()
四、编写demo脚本
# coding=utf-8
"""@Project :playwright_env @File :test.py@Author :gaojs@Date :2022/8/20 18:07@Blogs : https://www.gaojs.com.cn
"""
from playwright.sync_api import Playwright, sync_playwright, expect
import playwright
import time# 创建playwright对象
playwright = sync_playwright().start()
# headless:默认为true,无头模式 # devtools默认为false:开发者工具默认关闭
# 浏览器对象:, args=["--start-maximized"]最大化没生效
browser = playwright.chromium.launch(headless=False, devtools=False)
# 上下文管理器对象
context = browser.new_context(viewport={'width': 1920, 'height': 1080})# 页面对象
page1 = context.new_page()page1.set_default_navigation_timeout(20000)# 打开浏览器
page1.goto('https://www.gaojs.com.cn')
# 重新加载
page1.reload()
# 等待页面某个元素出现
page1.wait_for_selector('//*[@id="Joe"]/header/div[1]/div/nav/a[7]')# page1.locator().click()# Click text=图床 >> nth=0
with page1.expect_popup() as popup_info:page1.locator("text=图床").first.click()
page2 = popup_info.value
page2.wait_for_url("http://image.gaojs.com.cn/explore/trending")# Click text=探索
page2.locator("text=探索").click()
# d等待页面右上角搜索按钮出现
page2.click("text=最近")
time.sleep(5)# Close page
page1.close()
# Close page
page2.close()
# ---------------------
context.close()
browser.close()
五、录制案例(回放稍作调试即可)
1.自动发送博客
from playwright.sync_api import Playwright, sync_playwright, expectdef run(playwright: Playwright) -> None:browser = playwright.chromium.launch(headless=False)context = browser.new_context()# Open new pagepage = context.new_page()# Go to https://www.gaojs.com.cn/admin/index.htmlpage.goto("https://www.gaojs.com.cn/admin/index.html")# Go to https://www.gaojs.com.cn/admin/index.html#/page.goto("https://www.gaojs.com.cn/admin/index.html#/")# Go to https://www.gaojs.com.cn/admin/index.html#/login?redirect=%2Fdashboardpage.goto("https://www.gaojs.com.cn/admin/index.html#/login?redirect=%2Fdashboard")# Click [placeholder="用户名\/邮箱"]page.locator("[placeholder=\"用户名\\/邮箱\"]").click()# Fill [placeholder="用户名\/邮箱"]page.locator("[placeholder=\"用户名\\/邮箱\"]").fill("xxxxxxxx")# Press Tabpage.locator("[placeholder=\"用户名\\/邮箱\"]").press("Tab")# Press CapsLockpage.locator("[placeholder=\"密码\"]").press("CapsLock")# Fill [placeholder="密码"]page.locator("[placeholder=\"密码\"]").fill("B")# Press CapsLockpage.locator("[placeholder=\"密码\"]").press("CapsLock")# Fill [placeholder="密码"]page.locator("[placeholder=\"密码\"]").fill("xxxxxxxxxxx")# Click button:has-text("登 录")page.locator("button:has-text(\"登 录\")").click()page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/dashboard")# Click text=速记 发 布 >> textareapage.locator("text=速记 发 布 >> textarea").click()# Fill text=速记 发 布 >> textareapage.locator("text=速记 发 布 >> textarea").fill("playwright自动化进阶学习,微软新一代神器!!!")# Click button:has-text("发 布")page.locator("button:has-text(\"发 布\")").click()# Click ul[role="menu"] div:has-text("文章")page.locator("ul[role=\"menu\"] div:has-text(\"文章\")").click()# Click a:has-text("所有文章")page.locator("a:has-text(\"所有文章\")").click()page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/posts/list?page=0&size=10&statuses=PUBLISHED&statuses=DRAFT&statuses=INTIMATE")# Click span:nth-child(4)page.locator("span:nth-child(4)").click()# Click a:has-text("个人资料")page.locator("a:has-text(\"个人资料\")").click()page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/user/profile")# Click div:has-text("系统") >> nth=2page.locator("div:has-text(\"系统\")").nth(2).click()# Click a:has-text("博客设置")page.locator("a:has-text(\"博客设置\")").click()page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/system/options")# Click a:has-text("仪表盘")page.locator("a:has-text(\"仪表盘\")").click()page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/dashboard")# Click span:nth-child(4)page.locator("span:nth-child(4)").click()# Click text=退出登录page.locator("text=退出登录").click()# Click button:has-text("确 定")page.locator("button:has-text(\"确 定\")").click()page.wait_for_url("https://www.gaojs.com.cn/admin/index.html#/login?redirect=%2Fdashboard")# Close pagepage.close()# ---------------------context.close()browser.close()with sync_playwright() as playwright:run(playwright)
2.网易云音乐签到
import timefrom playwright.sync_api import Playwright, sync_playwright, expectdef run(playwright: Playwright) -> None:browser = playwright.chromium.launch(headless=False)context = browser.new_context(viewport={'width': 1920, 'height': 1080})# Open new pagepage = context.new_page()# Go to https://music.163.com/page.goto("https://music.163.com/")# Click text=用户登录page.frame_locator("iframe[name=\"contentFrame\"]").locator("text=用户登录").click()# Click text=选择其他登录模式page.locator("text=选择其他登录模式").click()# Check input[type="checkbox"]page.locator("input[type=\"checkbox\"]").check()# Click text=QQ登录with page.expect_popup() as popup_info:page.locator("text=QQ登录").click()page1 = popup_info.value# Click #img_out_1170527913page1.frame_locator("iframe[name=\"ptlogin_iframe\"]").locator("#img_out_1170527913").click()# 因为token一直在变,所有后面内容使用*代替page1.wait_for_url(f"https://music.163.com/back/sns?key=*")# Close pagepage1.close()# Click text=我的音乐page.locator("text=我的音乐").click()page.wait_for_url("https://music.163.com/#/my/m/music/playlist?id=152392364")time.sleep(3)# Click a:has-text("播放")page.frame_locator("iframe[name=\"contentFrame\"]").locator("a:has-text(\"播放\")").click()time.sleep(10)# Click text=网易云音乐page.locator("text=网易云音乐").click()page.wait_for_url("https://music.163.com/#")# Click a:has-text("签到")page.frame_locator("iframe[name=\"contentFrame\"]").locator('//*[@id="discover-module"]/div[2]/div[1]/div/div/div/div/a/i').click()time.sleep(10)# Close pagepage.close()# ---------------------context.close()browser.close()print('******************************* 签到完成 *******************************')with sync_playwright() as playwright:run(playwright)
3.Bilibili签到
from playwright.sync_api import Playwright, sync_playwright, expectdef run(playwright: Playwright) -> None:browser = playwright.chromium.launch(headless=False)context = browser.new_context(viewport={'width': 1920, 'height': 1080})# Open new pagepage = context.new_page()# Go to https://www.bilibili.com/page.goto("https://www.bilibili.com/")# Click span:has-text("登录")page.locator("span:has-text(\"登录\")").click()# Click text=QQ登录with page.expect_popup() as popup_info:page.locator("text=QQ登录").click()page1 = popup_info.value# Click text=1170527913 懿曲折扇情page1.frame_locator("iframe[name=\"ptlogin_iframe\"]").locator("text=1170527913 懿曲折扇情").click()page1.wait_for_url("https://www.bilibili.com/")# Click text=创作中心with page1.expect_popup() as popup_info:page1.locator("text=创作中心").click()page2 = popup_info.value# Click #canvas-wrap img >> nth=2page2.locator("#canvas-wrap img").nth(2).click()# Click #canvas-wrap img >> nth=2page2.locator("#canvas-wrap img").nth(2).click()# Click #canvas-wrap img >> nth=2page2.locator("#canvas-wrap img").nth(2).click()# Click #canvas-wrap img >> nth=1page2.locator("#canvas-wrap img").nth(1).click()page2.hover('//*[@id="app"]/div[1]/div/div[2]/span[1]/a/img')# Click text=直播中心with page2.expect_popup() as popup_info:page2.locator("text=直播中心").click()page3 = popup_info.value# Click text=签到 >> nth=0page3.locator("text=签到").first.click()# Click span:has-text("22") >> nth=0page3.locator('//div[@class="checkin-btn t-center pointer"]').first.click()# Close pagepage3.close()# Close pagepage2.close()# Close pagepage1.close()# Close pagepage.close()# ---------------------context.close()browser.close()with sync_playwright() as playwright:run(playwright)
六、回放高可用性
B站视频
七、自动等待Options
智能等待API:
element_handle.is_checked()
element_handle.is_disabled()
element_handle.is_editable()
element_handle.is_enabled()
element_handle.is_hidden()
element_handle.is_visible()
page.is_checked(selector, **kwargs)
page.is_disabled(selector, **kwargs)
page.is_editable(selector, **kwargs)
page.is_enabled(selector, **kwargs)
page.is_hidden(selector, **kwargs)
page.is_visible(selector, **kwargs)
locator.is_checked(**kwargs)
locator.is_disabled(**kwargs)
locator.is_editable(**kwargs)
locator.is_enabled(**kwargs)
locator.is_hidden(**kwargs)
locator.is_visible(**kwargs)
八、断言options
expect(locator).not_to_be_checked(**kwargs)
expect(locator).not_to_be_disabled(**kwargs)
expect(locator).not_to_be_editable(**kwargs)
expect(locator).not_to_be_empty(**kwargs)
expect(locator).not_to_be_enabled(**kwargs)
expect(locator).not_to_be_focused(**kwargs)
expect(locator).not_to_be_hidden(**kwargs)
expect(locator).not_to_be_visible(**kwargs)
expect(locator).not_to_contain_text(expected, **kwargs)
expect(locator).not_to_have_attribute(name, value, **kwargs)
expect(locator).not_to_have_class(expected, **kwargs)
expect(locator).not_to_have_count(count, **kwargs)
expect(locator).not_to_have_css(name, value, **kwargs)
expect(locator).not_to_have_id(id, **kwargs)
expect(locator).not_to_have_js_property(name, value, **kwargs)
expect(locator).not_to_have_text(expected, **kwargs)
expect(locator).not_to_have_value(value, **kwargs)
expect(locator).not_to_have_values(values, **kwargs)
expect(locator).to_be_checked(**kwargs)
expect(locator).to_be_disabled(**kwargs)
expect(locator).to_be_editable(**kwargs)
expect(locator).to_be_empty(**kwargs)
expect(locator).to_be_enabled(**kwargs)
expect(locator).to_be_focused(**kwargs)
expect(locator).to_be_hidden(**kwargs)
expect(locator).to_be_visible(**kwargs)
expect(locator).to_contain_text(expected, **kwargs)
expect(locator).to_have_attribute(name, value, **kwargs)
expect(locator).to_have_class(expected, **kwargs)
expect(locator).to_have_count(count, **kwargs)
expect(locator).to_have_css(name, value, **kwargs)
expect(locator).to_have_id(id, **kwargs)
expect(locator).to_have_js_property(name, value, **kwargs)
expect(locator).to_have_text(expected, **kwargs)
expect(locator).to_have_value(value, **kwargs)
expect(locator).to_have_values(values, **kwargs)
expect(page).not_to_have_title(title_or_reg_exp, **kwargs)
expect(page).not_to_have_url(url_or_reg_exp, **kwargs)
expect(page).to_have_title(title_or_reg_exp, **kwargs)
expect(page).to_have_url(url_or_reg_exp, **kwargs)
expect(api_response).not_to_be_ok()
expect(api_response).to_be_ok()
案例
page.locator("#submit-button").click()expect(page.locator(".status")).to_have_text("Submitted")
playwright使用心得相关推荐
- Java EE学习心得
–Java EE学习心得 1. 称为编程专家的秘诀是: 思考-----编程--------思考------编程--.. 编程不能一步到位,不能一上来就编,必须先思考如何写,怎样写?然后再编程 ...
- 测试心得:微图书销售小程序
测试心得:微图书销售小程序 前言 这个学期差不多也将近结束,经过大半个学期,从项目需求的确认和项目文档的编写,到一步步的设计与实现,现在终于到了测试阶段,但是我们在测试阶段也暴露出了很多bug,但是每 ...
- java.lang.OutOfMemoryError:GC overhead limit exceeded填坑心得
该文章出自:http://www.cnblogs.com/hucn/p/3572384.html 分析工具:http://www.blogjava.net/jjshcc/archive/2014/03 ...
- 计算机财务应用实验心得,计算机会计实习心得-20210628124643.doc-原创力文档
计算机会计实习心得 计算机会计实习心得1 毕业实践环节是大学生在完成全部课程后.走向社会之前最真实的一个模拟实验,对于我们财会专业的学生,平时注意注重理论学习,缺乏实践锻炼,因此实习显得尤为重要.在本 ...
- html5考试总结300字,期中考心得300字5
为了检验学生半个学期所学的知识而进行的一次考试,有利于学生比较正式地检验自己平时的学习水平,根据这个成绩,学生可以及时的调整学习心态和方法,更有效率地进行下一阶段的学习,期中考试主要考察学生前半学期的 ...
- Assembly学习心得
http://blog.csdn.net/etmonitor/ Assembly学习心得 说明: 最近开始准备把学到的.NET知识重新整理一遍,眼过千遍不如手过一遍,所以我准备记下我的学习心得,已备参 ...
- 什么叫安装文件索引服务器,搜出精彩 玩转Windows 2008系统心得
[IT168 专稿]不少朋友已经在不经意间与Windows Server 2008系统进行了亲密接触,在一段时间的接触之后,不知大家对该系统的文件搜索功能会有什么样的体会?其实,Windows Ser ...
- mysql主从数据库含义_(转)Mysql数据库主从心得整理
管理mysql主从有2年多了,管理过200多组mysql主从,几乎涉及到各个版本的主从,本博文属于总结性的,有一部分是摘自网络,大部分是根据自己管理的心得和经验所写,整理了一下,分享给各位同行,希望对 ...
- 工作中InnoDB引擎数据库主从复制同步心得
近期将公司的MySQL架构升级了,由原先的一主多从换成了DRBD+Heartbeat双主多从,正好手上有一个电子商务网站新项目也要上线了,用的是DRBD+Heartbeat双主一从,由于此过程还是有别 ...
最新文章
- 找对业务G点, 体验酸爽 - PostgreSQL内核扩展指南
- 002_centos7上安装mysql8
- linux 编译窗口,Ubuntu 10.04上编译安装Sawfish窗口管理器
- oracle 存long,ORACLE中LONG类型字段的存取
- 深度召回算法在字节跳动推荐系统的应用实践
- wps python 自动化_用Python控制Excel实现自动化办公,手把手教你
- javatodo框架中怎么配置路由
- 重构第0天--重构的理解
- java batik 字体文件_用 Apache batik 1.10 把svg代码转成png图片,文字丢失???
- 微软部分专卖店地址选定 今秋开张
- 10 craps赌博游戏
- 理解神经网络:神经元的概念
- 一个80后神话的倒掉----郑立
- virtualbox安装.img文件
- 使用application记录页面访问次数
- Spark中资源与任务的关系
- 国家天地图API 创建面 覆盖物
- 算法中的微积分:5大函数求导公式让你在面试中脱颖而出
- 武侠大宗师 本机搭建, 自玩
- 1万条数据大概占多大空间_mysql亿级数据数据库优化方案测试-银行交易流水记录的查询...