小言_互联网的博客

微软开源的浏览器自动化工具-Playwright

392人阅读  评论(0)

软开源了一个 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的方式上有所不同。

同步:


   
  1. from playwright import sync_playwright
  2. 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()

异步:


   
  1. import asynciofrom playwright import async_playwright
  2. 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()
  3. 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、交互模式运行:


   
  1. >>> from playwright import sync_playwright>>> playwright = sync_playwright().start()
  2. # 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("http://whatsmyuseragent.org/")>>> page.screenshot(path="example.png")>>> browser.close()>>> playwright.stop(

4、执行 JS 代码:


   
  1. from playwright import sync_playwright
  2. 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、中断网络请求:


   
  1. from playwright import sync_playwright
  2. with sync_playwright() as p:
  3. browser = p.chromium.launch()
  4. page = browser.newPage()
  5. def log_and_continue_request(route, request):
  6. print(request.url)
  7. route.continue_()
  8. # Log and continue all network requests
  9. page.route( '**', lambda route, request: log_and_continue_request(route, request))
  10. page.goto( 'http://todomvc.com')
  11. browser.close()

官方文档暂时还是 Node.js 版本,不过正在转换成 Python 版本,API的调用方式相当一致,现在看 Node.js 版本的文档来编码也是无障碍的。

官方文档:https://playwright.dev/

GitHub 仓库:https://github.com/microsoft/playwright-python

综上,感觉比 selenium 更好用啦,准备入坑。如果觉得文章对你有用,欢迎点赞、转发、关注,谢谢支持。


转载:https://blog.csdn.net/somenzz/article/details/109396506
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场