软开源了一个Python项目:Playwright,从此又多了一个浏览器自动化工具。之前一直用selenium或splinter。
Playwright 可通过单个API自动执行Chromium,Firefox和WebKit浏览器,支持无头浏览器(headless),Linux、macOS、Windows下均可以使用,Playwright提供的自动化技术是绿色的,功能强大,稳定且速度快。
Playwright最吸引我的地方在于它可以自己记录你对浏览器的操作,并将这些操作生成可以执行的代码,这简直就是神器,大大提升了浏览器自动化的效率。生成代码只需要执行
python -m playwright codegen
此外,它不像 selenium 需要再单独安装浏览器驱动,它在 pip install 时就会安装浏览器的驱动文件。
pip install playwrightpython -m playwright install
这将会安装 Playwright 和 Chromium,Firefox 和 WebKit 浏览器的二进制文件,非常方便,需要 Python 3.7 及以上版本。
还有几个亮眼的功能:
1、Playwright同时提供同步(阻止)API和异步API。
它们在功能方面是相同的,只是在使用API的方式上有所不同。
同步:
from playwright import sync_playwright with sync_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: browser = browser_type.launch() page = browser.newPage() page.goto('http://whatsmyuseragent.org/') page.screenshot(path=f'example-{browser_type.name}.png') browser.close()
异步:
import asynciofrom playwright import async_playwright async def main(): async with async_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: browser = await browser_type.launch() page = await browser.newPage() await page.goto('http://whatsmyuseragent.org/') await page.screenshot(path=f'example-{browser_type.name}.png') await browser.close() asyncio.get_event_loop().run_until_complete(main())
2、集成 pytest 测试:
def test_playwright_is_visible_on_google(page): page.goto("https://www.google.com") page.type("input[name=q]", "Playwright GitHub") page.click("input[type=submit]") page.waitForSelector("text=microsoft/Playwright")
3、交互模式运行:
>>> from playwright import sync_playwright >>> playwright = sync_playwright().start() # Use playwright.chromium, playwright.firefox or playwright.webkit # Pass headless=False to see the browser UI >>> browser = playwright.chromium.launch() >>> page = browser.newPage() >>> page.goto(" >>> page.screenshot(path="example.png")>>> browser.close()>>> playwright.stop(
4、执行 JS 代码:
from playwright import sync_playwright with sync_playwright() as p: browser = p.firefox.launch() page = browser.newPage() page.goto('https://www.example.com/') dimensions = page.evaluate('''() => { return { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight, deviceScaleFactor: window.devicePixelRatio } }''') print(dimensions) browser.close()
5、中断网络请求:
from playwright import sync_playwright with sync_playwright() as p: browser = p.chromium.launch() page = browser.newPage() def log_and_continue_request(route, request): print(request.url) route.continue_() # Log and continue all network requests page.route('**', lambda route, request: log_and_continue_request(route, request)) page.goto('http://todomvc.com') browser.close()
官方文档暂时还是 Node.js 版本,不过正在转换成 Python 版本,API的调用方式相当一致,现在看 Node.js 版本的文档来编码也是无障碍的。
作者:somenzz
原文链接:https://blog.csdn.net/somenzz/article/details/109396506