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, --output <file name> saves the generated script to a file--target <language> language 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/
© 版权声明
文章版权归作者所有,未经允许请勿转载。