1 Introduction
Recently, Microsoft opened up a very powerful Python automation dependency library: playwright-python
It supports mainstream browsers, including: Chrome, Firefox, Safari, Microsoft Edge, etc., and also supports running in headless mode and headless mode
playwright-python provides both synchronous and asynchronous APIs, which can be used in conjunction with the Pytest test framework, and automatic script recording on the browser side is also supported.
Project address: https://github.com/microsoft/playwright-python
2. Preparation
Before the actual combat, we only need 2 steps
Step 1. Install playwright-python dependent libraries
# 安装依赖库
pip3 install playwright
Step 2. Install the mainstream browser driver
In this way, Chromeium, Firefox, Webkit browser drivers will be downloaded to the local
# 安装浏览器驱动
python -m playwright install
3. Take a look
3-1 Recording script
Let's first review the command description of the recording script
among them
· Python -m playwright codegen recording script
· --Help Help documentation
· -O automatic script directory generation
· --Target scripting language, including JS and Python, the corresponding values are: python and javascript
· -B specifies the browser driver
比如
# 我们通过下面命令打开 Chrome 浏览器开始录制脚本
# 指定生成语言为:Python(默认Python,可选)
# 保存的文件名:1.py(可选)
# 浏览器驱动:webkit(默认webkit,可选)
# 最后跟着要打开的目标网站(默认仅仅是打开浏览器,可选)
python -m playwright codegen --target python -o 1.py -b webkit https://www.baidu.com
Next, simulate the operation of searching once in the browser, and then close the browser
Python learning exchange group . Welcome friends to exchange and learn.
Finally, the automated script will be automatically generated and saved to a file
from playwright import sync_playwright
def run(playwright):browser =
playwright.webkit.launch(headless=False)context =
browser.newContext
# Open new pagepage = context.newPage
# Go to https://www.baidu.com/page.goto("https://www.baidu.com/")
# Fill input[name="wd"]
page.fill("input[name="wd"]", "AirPython")
# Press Enter# with
page.expect_navigation(url="https://www.baidu.com/s?ie=utf-
8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=AirPython&fenlei=256&rsv_pq=a1739d870005eec3&rsv_t=e640
wwS33ra1Koivxvy1WyTxyknRwnllWiw4JBqIYd/KUN/WKpWLtL2b2+0&rqlang=cn&rsv_enter=1&rsv_dl=tb&r
sv_sug3=21&rsv_sug1=18&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=6199&rsv_sug4=6199"):
with page.expect_navigation:page.press("input[name="wd"]", "Enter")
# Close pagepage.close
# ---------------------
context.close
browser.close
with sync_playwright as playwright:
run(playwright)
3-2 Synchronization
The synchronization keyword is: sync_playwright
For example, we open the browser through the three browser cores in turn, then click Baidu, then take a screenshot of the search interface, and finally close the browser
from time import sleep from playwright import sync_playwright
# 注意:默认是无头模式
with sync_playwright as p:
# 分别对应三个浏览器驱动
forbrowser_type in [p.chromium, p.firefox, p.webkit]:
# 指定为有头模式,方便查看
browser = browser_type.launch(headless=False)
page = browser.newPagepage.goto( http://baidu.com )
# 执行一次搜索操作
page.fill("input[name="wd"]", "AirPython")
with page.expect_navigation:
page.press("input[name="wd"]", "Enter")
# 等待页面加载完全
page.waitForSelector("text=百度热榜")
# 截图
page.screenshot(path=f example-{browser_type.name}.png )
# 休眠
5s sleep(5)
# 关闭浏览器
browser.close
It should be pointed out that the common automated operations of playwright-python's built-in API are basically covered.
3-3 Asynchronous
The asynchronous keyword is: async_playwright
Combined with asyncio, we perform the above operations at the same time
import asyncio from playwright import async_playwright
# 异步执行
async def main:
async with async_playwright as p:
forbrowser_type in [p.chromium, p.firefox, p.webkit]:
# 指定为有头模式,方便查看
browser = await browser_type.launch(headless=False)
page = await browser.newPageawait page.goto( http://baidu.com )
# 执行一次搜索操作
await page.fill("input[name="wd"]", "AirPython")
await page.press("input[name="wd"]", "Enter")
# 等待页面加载完全
await page.waitForSelector("text=百度热榜")
# 截图
await page.screenshot(path=f example-{browser_type.name}.png )
await browser.closeasyncio.get_event_loop.run_until_complete(main)
4. Finally
In fact, Playwright is a cross-language automation framework that supports Python, Java, JS, etc.
Compared with the traditional automation framework Selenium, Playwright is more concise and powerful in context and API usage.