话接上回,本文介绍下python环境下的基础操作
安装
# 先安装pytest-playwright插件
pip install pytest-playwright
# 再安装playwright所需要的浏览器内核
playwright install
执行完上面两个命令,等待下载好playwright所需要的浏览器内核,就可以使用了
系统要求
- Python 3.8 +.
- Windows 10+, Windows Server 2016+ or Windows Subsystem for Linux (WSL).
- MacOS 12 Monterey, MacOS 13 Ventura, or MacOS 14 Sonoma.
- Debian 11, Debian 12, Ubuntu 20.04 or Ubuntu 22.04.
Demo脚本
新建一个python文件,如pw_demo.py
import re
from playwright.sync_api import Page, expect
def test_has_title(page: Page):
# 打开一个网页
page.goto("https://playwright.dev/")
# 预期网页的title为'Playwright'
expect(page).to_have_title(re.compile("Playwright"))
def test_get_started_link(page: Page):
page.goto("https://playwright.dev/")
# 点击页面上的链接
page.get_by_role("link", name="Get started").click()
# 预期网页有head为'Installation'的元素可以被看见
expect(page.get_by_role("heading", name="Installation")).to_be_visible()
执行一下就可以看到效果了,快去试试~~~!