浏览器自动化工具--playwright
浏览器自动化工具--playwright
【待验证...】
安装
两步:
# 安装playwright库 pip install playwright # 安装浏览器驱动文件(安装过程稍微有点慢) python -m playwright install
依赖
- 安装Playwright依赖库,需要Python3.7+
- 安装Chromium、Firefox、WebKit等浏览器的驱动文件
###############################
# 【在win10-x64系统上验证操作过程】
###############################
C:\Users\limx>pip install playwright
Collecting playwright
Downloading playwright-1.22.0-py3-none-win_amd64.whl (26.5 MB)
---------------------------------------- 26.5/26.5 MB 1.1 MB/s eta 0:00:00
Collecting greenlet==1.1.2
Downloading greenlet-1.1.2-cp310-cp310-win_amd64.whl (101 kB)
---------------------------------------- 102.0/102.0 KB 650.7 kB/s eta 0:00:00
Collecting pyee==8.1.0
Downloading pyee-8.1.0-py2.py3-none-any.whl (12 kB)
Collecting websockets==10.1
Downloading websockets-10.1-cp310-cp310-win_amd64.whl (97 kB)
---------------------------------------- 97.6/97.6 KB 801.1 kB/s eta 0:00:00
Installing collected packages: pyee, websockets, greenlet, playwright
Successfully installed greenlet-1.1.2 playwright-1.22.0 pyee-8.1.0 websockets-10.1
C:\Users\limx>python -m playwright install
Downloading Chromium 102.0.5005.40 (playwright build v1005) - 104.1 Mb [====================] 100% 0.0s
(node:233444) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Chromium 102.0.5005.40 (playwright build v1005) downloaded to C:\Users\limx\AppData\Local\ms-playwright\chromium-1005
Downloading FFMPEG playwright build v1007 - 1.4 Mb [====================] 100% 0.0s
FFMPEG playwright build v1007 downloaded to C:\Users\limx\AppData\Local\ms-playwright\ffmpeg-1007
Downloading Firefox 99.0.1 (playwright build v1323) - 76 Mb [====================] 100% 0.0s
Firefox 99.0.1 (playwright build v1323) downloaded to C:\Users\limx\AppData\Local\ms-playwright\firefox-1323
Downloading Webkit 15.4 (playwright build v1641) - 43 Mb [====================] 100% 0.0s
Webkit 15.4 (playwright build v1641) downloaded to C:\Users\limx\AppData\Local\ms-playwright\webkit-1641
录制操作
# 命令行键入 --help 可看到所有选项
python -m playwright codegen
|
options含义:
-o:将录制的脚本保存到一个文件
--target:规定生成脚本的语言,有JS和Python两种,默认为Python
-b:指定浏览器驱动
比如,我要在baidu.com搜索,用chromium驱动,将结果保存为my.py的python文件。
python -m playwright codegen --target python -o 'my.py' -b chromium https://www.baidu.com
移动端
playwright可支持移动端的浏览器模拟。