注意:我这里使用虚拟环境
playwright官方文档(python)
使用总结:开箱即用
支持录制,脚本清晰,方便维护
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# 语言
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
# Open new page
page = context.new_page()
# Go to https://www.gaojs.com.cn/
page.goto("https://www.gaojs.com.cn/")
# Click text=关于作者 >> nth=0
page.locator("text=关于作者").first.click()
page.wait_for_url("https://www.gaojs.com.cn/s/about")
# Click text=图床 >> nth=0
with page.expect_popup() as popup_info:
page.locator("text=图床").first.click()
page1 = popup_info.value
page.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=0
page.locator("a:has-text(\"Music\")").first.click()
page.wait_for_url("https://www.gaojs.com.cn/s/music")
# Click svg >> nth=0
page.locator("svg").first.click()
# Close page
page1.close()
# Close page
page.close()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
# Click text=图床 >> nth=0
with page.expect_popup() as popup_info:
page.locator("text=图床").first.click()
page1 = popup_info.value
# 这里需要改一下,录制出来是page,其实是page1
page1.wait_for_url("http://image.gaojs.com.cn/explore/trending")
# Click text=探索
page1.locator("text=探索").click()
# 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()
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
# Open new page
page = context.new_page()
# 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#/
page.goto("https://www.gaojs.com.cn/admin/index.html#/")
# Go to https://www.gaojs.com.cn/admin/index.html#/login?redirect=%2Fdashboard
page.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 Tab
page.locator("[placeholder=\"用户名\\/邮箱\"]").press("Tab")
# Press CapsLock
page.locator("[placeholder=\"密码\"]").press("CapsLock")
# Fill [placeholder="密码"]
page.locator("[placeholder=\"密码\"]").fill("B")
# Press CapsLock
page.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=速记 发 布 >> textarea
page.locator("text=速记 发 布 >> textarea").click()
# Fill text=速记 发 布 >> textarea
page.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=2
page.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 page
page.close()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
import time
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context(viewport={'width': 1920, 'height': 1080})
# Open new page
page = 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_1170527913
page1.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 page
page1.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 page
page.close()
# ---------------------
context.close()
browser.close()
print('******************************* 签到完成 *******************************')
with sync_playwright() as playwright:
run(playwright)
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context(viewport={'width': 1920, 'height': 1080})
# Open new page
page = 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=2
page2.locator("#canvas-wrap img").nth(2).click()
# Click #canvas-wrap img >> nth=2
page2.locator("#canvas-wrap img").nth(2).click()
# Click #canvas-wrap img >> nth=2
page2.locator("#canvas-wrap img").nth(2).click()
# Click #canvas-wrap img >> nth=1
page2.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=0
page3.locator("text=签到").first.click()
# Click span:has-text("22") >> nth=0
page3.locator('//div[@class="checkin-btn t-center pointer"]').first.click()
# Close page
page3.close()
# Close page
page2.close()
# Close page
page1.close()
# Close page
page.close()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
B站视频
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)
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")