前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微软自动化神器playwright

微软自动化神器playwright

作者头像
懿曲折扇情
发布2022-08-24 13:07:20
3.1K1
发布2022-08-24 13:07:20
举报
文章被收录于专栏:建帅技术分享

注意:我这里使用虚拟环境

一、playwright官网(非常详细)

playwright官方文档(python)

二、playwright优势

代码语言:javascript
复制
使用总结:开箱即用
支持录制,脚本清晰,方便维护

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.当我不知道有些方法的使用过程的时候,我会录制脚本查看脚本内容

代码语言:javascript
复制
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)

2.点击跳转新页面(由录制脚本1内容可知道)

代码语言:javascript
复制
    # 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()

四、编写demo脚本

代码语言:javascript
复制
# 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.自动发送博客

代码语言:javascript
复制
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)

2.网易云音乐签到

代码语言:javascript
复制
# coding=utf-8
"""
    @project: automation_tools
    @Author:gaojs
    @file: test008.py
    @date:2022/8/17 17:36
    @blogs: https://www.gaojs.com.cn
"""
import requests


def dayly_news(news_type):
    """
    每日新闻:news_type	支持类型top(推荐,默认)guonei(国内)guoji(国际)yule(娱乐)tiyu(体育)junshi(军事)keji(科技)caijing(财经)youxi(游戏)qiche(汽车)jiankang(健康)
    """
    url = f'http://v.juhe.cn/toutiao/index?type={news_type}&page=1&page_size=30&is_filter=0&key=cb53bd41a74ef445a2d1b7fcfebe6fa0'
    resp = requests.get(url=url)
    with open('title.txt', mode='a+', encoding='GBK') as fin:
        fin.write(news_type + ' 新闻 ' + '\n')
        fin.write('\n')
    t = eval(resp.text)
    try:
        for i in range(30):
            s = t.get('result').get('data')[i].get('title')
            # i = i + 1
            with open('title.txt', mode='a+', encoding='GBK') as fin:
                fin.write(str(i+1) + '.' + s + '\n')
    except Exception as e:
        print(e)
    finally:
        with open('title.txt', mode='a+', encoding='GBK') as fin:
            fin.write('\n')


if __name__ == '__main__':
    dayly_news('guonei')
    dayly_news('guoji')
    dayly_news('yule')
    dayly_news('tiyu')
    dayly_news('junshi')
    dayly_news('keji')
    dayly_news('caijing')
    dayly_news('youxi')
    dayly_news('qiche')
    dayly_news('jiankang')

3.Bilibili签到

代码语言:javascript
复制
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站视频

https://www.bilibili.com/video/BV14B4y1z7Ev

七、自动等待Options

智能等待API:

代码语言:javascript
复制
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

代码语言:javascript
复制
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()

案例

代码语言:javascript
复制
    page.locator("#submit-button").click()
    expect(page.locator(".status")).to_have_text("Submitted")
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-08-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、playwright官网(非常详细)
  • 二、playwright优势
  • 三、录制脚本
    • 1.当我不知道有些方法的使用过程的时候,我会录制脚本查看脚本内容
      • 2.点击跳转新页面(由录制脚本1内容可知道)
      • 四、编写demo脚本
      • 五、录制案例(回放稍作调试即可)
        • 1.自动发送博客
          • 2.网易云音乐签到
            • 3.Bilibili签到
            • 六、回放高可用性
            • 七、自动等待Options
              • 智能等待API:
              • 八、断言options
                • 案例
                相关产品与服务
                云开发 CLI 工具
                云开发 CLI 工具(Cloudbase CLI Devtools,CCLID)是云开发官方指定的 CLI 工具,可以帮助开发者快速构建 Serverless 应用。CLI 工具提供能力包括文件储存的管理、云函数的部署、模板项目的创建、HTTP Service、静态网站托管等,您可以专注于编码,无需在平台中切换各类配置。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档