playwright入门

最近发现了一个自动化测试的神器。
官网地址:https://playwright.dev/docs/why-playwright/

Playwright提供了一组API,可自动执行Chromium,Firefox和WebKit浏览器。
通过使用Playwright API,您可以编写Python代码来创建新的浏览器页面,导航到URL,然后与页面上的元素进行交互。
Playwright可以与测试运行程序一起用于自动进行用户交互,以验证和测试Web应用程序。
Playwright API通过以下原语实现此目的。

  • 打开浏览器
  • 浏览器上下文
  • 页面和框架
  • 选择器
  • 自动等待
  • 执行上下文:Node.js和浏览器
  • 对象和元素句柄

一、安装

使用阿里源,下载速度快一点。
pip install playwright -i https://mirrors.aliyun.com/pypi/simple/

安装Chromium、Firefox、WebKit等浏览器的驱动文件
python -m playwright install

二、录制

使用Playwright无需写一行代码,我们只需手动操作浏览器,它会录制我们的操作,然后自动生成代码脚本。
下面就是录制的命令codegen,仅仅一行。
python -m playwright codegen

举例:我要在baidu.com搜索,用chromium驱动,将结果保存为my.py的python文件。

python -m playwright codegen --target python -o my.py -b chromium https://www.baidu.com

下面的例子,展示的打开百度,进行搜索。系统自动录制操作动作,并生成python脚本

from playwright import sync_playwright

def run(playwright):
    browser = playwright.chromium.launch(headless=False)
    context = browser.newContext()

    # Open new page
    page = context.newPage()

    # Go to https://www.baidu.com/
    page.goto("https://www.baidu.com/")

    # Click input[name="wd"]
    page.click("input[name=\"wd\"]")

    # Fill input[name="wd"]
    page.fill("input[name=\"wd\"]", "宝马汽车")

    # Go to https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E8%9A%82%E8%9A%81%E6%B2%89%E9%A6%99&fenlei=256&rsv_pq=9922438e0001e6c4&rsv_t=80e30fK9WkdiBWynXeqiNJIl80Q0ZcAuB3ZzCZU6IIcnQXPoLhm4khTU79A&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=14&rsv_sug1=16&rsv_sug7=100&rsv_btype=i&prefixsug=%25E8%259A%2582%25E8%259A%2581%25E6%25B2%2589%25E9%25A6%2599&rsp=5&inputT=6054&rsv_sug4=6670
    page.goto("https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E8%9A%82%E8%9A%81%E6%B2%89%E9%A6%99&fenlei=256&rsv_pq=9922438e0001e6c4&rsv_t=80e30fK9WkdiBWynXeqiNJIl80Q0ZcAuB3ZzCZU6IIcnQXPoLhm4khTU79A&rqlang=cn&rsv_enter=0&rsv_dl=tb&rsv_sug3=14&rsv_sug1=16&rsv_sug7=100&rsv_btype=i&prefixsug=%25E8%259A%2582%25E8%259A%2581%25E6%25B2%2589%25E9%25A6%2599&rsp=5&inputT=6054&rsv_sug4=6670")

    page1 = context.newPage()
    # Go to https://zhidao.baidu.com/question/615937275180020252.html
    page1.goto("https://zhidao.baidu.com/question/615937275180020252.html")

    # Double click div[id="answer-content-1510052568"]
    page1.dblclick("div[id=\"answer-content-1510052568\"]")
    print(page1.element("div[id=\"answer-content-1510052568\"]").innertext())
    # Close page
    page1.close()

    # Close page
    page.close()

    # ---------------------
    context.close()
    browser.close()

with sync_playwright() as playwright:
    run(playwright)

三、同步

下面示例代码:依次打开三个浏览器,前往baidu搜索,截图后退出。

from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
    browser = browser_type.launch()
    page = browser.newPage()
    page.goto('https://baidu.com/')
    page.screenshot(path=f'example-{browser_type.name}.png')
    browser.close()

四、异步

异步操作可结合asyncio同时进行三个浏览器操作。

import asyncio
from playwright import async_playwright
async def main():
async with async_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = await browser_type.launch()
page = await browser.newPage()
await page.goto('http://baidu.com/')
await page.screenshot(path=f'example-{browser_type.name}.png')
await browser.close()
asyncio.get_event_loop().run_until_complete(main())

posted on 2021-01-19 14:28  耀扬  阅读(3477)  评论(0编辑  收藏  举报

导航