玫瑰花变蚊子血,自动化无痕浏览器对比测试

也许每一个男子全都有过这样的两个女人,至少两个 。娶了红玫瑰,久而久之,红的变了墙上的一抹蚊子血,白的还是床前明月光;娶了白玫瑰,白的便是衣服上沾的一粒饭黏子,红的却是心口上一颗朱砂痣 。–张爱玲《红玫瑰与白玫瑰》
一直都是开源自动化浏览器工具的王者,但这两年微软开源的异军突起,后来者居上,隐隐然有撼动江湖地位之势,本次我们来对比与之间的差异,看看曾经的玫瑰花是否会变成蚊子血 。
的安装和使用
是由业界大佬微软()开源的端到端 Web 测试和自动化库,可谓是大厂背书,功能满格,虽然作为无头浏览器,该框架的主要作用是测试 Web 应用,但事实上,无头浏览器更多的是用于 Web 抓取目的,也就是爬虫 。
首先终端运行安装命令:
pip3 install playwright
程序返回:
Successfully built greenletInstalling collected packages: pyee, greenlet, playwrightAttempting uninstall: greenletFound existing installation: greenlet 2.0.2Uninstalling greenlet-2.0.2:Successfully uninstalled greenlet-2.0.2Successfully installed greenlet-2.0.1 playwright-1.30.0 pyee-9.0.4
目前最新稳定版为1.30.0
随后可以选择直接安装浏览器驱动:
playwright install
程序返回:
Downloading Chromium 110.0.5481.38 (playwright build v1045) from https://playwright.azureedge.net/builds/chromium/1045/chromium-mac-arm64.zip123.8 Mb [====================] 100% 0.0sChromium 110.0.5481.38 (playwright build v1045) downloaded to /Users/liuyue/Library/Caches/ms-playwright/chromium-1045Downloading FFMPEG playwright build v1008 from https://playwright.azureedge.net/builds/ffmpeg/1008/ffmpeg-mac-arm64.zip1 Mb [====================] 100% 0.0sFFMPEG playwright build v1008 downloaded to /Users/liuyue/Library/Caches/ms-playwright/ffmpeg-1008Downloading Firefox 108.0.2 (playwright build v1372) from https://playwright.azureedge.net/builds/firefox/1372/firefox-mac-11-arm64.zip69.8 Mb [====================] 100% 0.0sFirefox 108.0.2 (playwright build v1372) downloaded to /Users/liuyue/Library/Caches/ms-playwright/firefox-1372Downloading Webkit 16.4 (playwright build v1767) from https://playwright.azureedge.net/builds/webkit/1767/webkit-mac-12-arm64.zip56.9 Mb [====================] 100% 0.0sWebkit 16.4 (playwright build v1767) downloaded to /Users/liuyue/Library/Caches/ms-playwright/webkit-1767
默认会下载内核、以及驱动 。
其中使用最广泛的就是基于内核的浏览器,最负盛名的就是的和微软自家的Edge 。
确保当前电脑安装了Edge浏览器,让我们小试牛刀一把:
from playwright.sync_api import sync_playwrightimport timewith sync_playwright() as p:browser = p.chromium.launch(channel="msedge", headless=True)page = browser.new_page()page.goto('http:/v3u.cn')page.screenshot(path=f'./example-v3u.png')time.sleep(5)browser.close()
这里导入模块,顾名思义,同步执行,通过上下文管理器开启浏览器进程 。
随后通过指定edge浏览器,截图后关闭浏览器进程:
我们也可以指定参数为True,让浏览器再后台运行:
from playwright.sync_api import sync_playwrightwith sync_playwright() as p:browser = p.chromium.launch(channel="msedge", headless=True)page = browser.new_page()page.goto('http:/v3u.cn')page.screenshot(path=f'./example-v3u.png')browser.close()

玫瑰花变蚊子血,自动化无痕浏览器对比测试

文章插图
除了同步模式,也支持异步非阻塞模式:
import asynciofrom playwright.async_api import async_playwrightasync def main():async with async_playwright() as p:browser = await p.chromium.launch(channel="msedge", headless=False)page = await browser.new_page()await page.goto("http://v3u.cn")print(await page.title())await browser.close()asyncio.run(main())