浏览器自动化工具--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

 

录制操作

使用Playwright无需写代码,只需手动操作浏览器,它会录制我们的操作,然后自动生成代码脚本。

# 命令行键入 --help 可看到所有选项
python -m playwright codegen

python -m playwright codegen --help
Usage: index codegen [options] [url]

open page and generate code for user actions

Options:
  -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 command

Examples:

  $ codegen
  $ codegen --target=python
  $ -b webkit codegen https://example.com

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

 
命令行输入后会自动打开浏览器,然后可以看见在浏览器上的一举一动都会被自动翻译成代码。
结束后自动关闭浏览器,保存生成的自动化脚本到py文件。
 
 
【运行试验】
执行:python -m playwright codegen -o mywebtest.py https://www.baidu.com
效果:比较卡,需要半分钟左右才弹出录制脚本的界面,然后出现打开浏览器的过程。
运行脚本:
python mywebtest.py
效果:启动较慢,10秒左右才打开浏览器。
 

移动端

playwright可支持移动端的浏览器模拟。

 

posted @ 2022-04-25 15:23  JamesLi_1119401255  阅读(958)  评论(0编辑  收藏  举报