playwright是一个强大的python库,仅用一个api即可自动执行chromium、firefox、webkit等主流浏览器自动化操作,并同时支持以无头模式、有头模式运行。
playwright提供的自动化技术是绿色的、功能强大、可靠且快速,支持linux、mac以及windows操作系统。
安装
playwright的安装非常简单,两步走。
# 安装playwright库pip install playwright# 安装浏览器驱动文件(安装过程稍微有点慢)python -m playwright install
上面两个pip操作分别安装:
- 安装playwright依赖库,需要python3.7
- 安装chromium、firefox、webkit等浏览器的驱动文件
录制
使用playwright无需写一行代码,我们只需手动操作浏览器,它会录制我们的操作,然后自动生成代码脚本。
下面就是录制的命令codegen,仅仅一行。
# 命令行键入 –help 可看到所有选项
python -m playwright codegen
codegen的用法可以使用--help查看,如果简单使用就是直接在命令后面加上url链接,如果有其他需要可以添加options。
python -m playwright codegen --helpusage: index codegen [options] [url]open page and generate code for user actionsoptions:-o, --outputsaves the generated script to a file --targetlanguage to use, one of javascript, python, python-async, csharp (default: "python") -h, --help display help for commandexamples:$ codegen$ codegen --target=python$ -b webkit codegen https://example.com
options含义:
- -o:将录制的脚本保存到一个文件
- –target:规定生成脚本的语言,有
js和python两种,默认为python - -b:指定浏览器驱动
项目链接
https://playwright.dev/
未经允许不得转载:凯发k8国际真人 » playwright是一个强大的python库安装教程