DEV Community

Yusuke Iwaki
Yusuke Iwaki

Posted on

Using 🎭Playwright in python:alpine

playwright-python is a really awesome library for browser automation.

However it doesn't support running on Alpine Linux.
https://github.com/microsoft/playwright-python/issues/234

Most web developers would use Alpine based Docker images, and actually playwright-python internally has a capability of running scripts in Alpine.

Why playwright-python cannot run on Alpine?

image

playwright-python comes with a browser downloader. As we know, browsers requires many dependencies. The deps are not compatible with Alpine.

This might be a main reason why playwright-python deny running on Alpine.

By the way, we already know Selenium Remote driver (Selenium Grid), it separates script runner and browser environments communicating with HTTP port (default: 4444).

How to separate script runner and browsers

image

playwright-python is just a Python client library of Playwright. Playwright server communicates with browser via WebSocket or other protocols, and playwright-python communicates with the server via pipe by default.

However playwright-python already introduced WebSocketTransport for the API BrowserType#connect() in Playwright v1.11 and later. It can be also used for Playwright connection between client and server.

Try it!

I created a tiny library.
https://github.com/YusukeIwaki/playwright-python-remote

This library provides a Playwright initializer (ContextManager) just replacing PipeTransport with WebSocketTransport.
https://github.com/YusukeIwaki/playwright-python-remote/blob/main/playwright_remote/sync_api/sync_playwright_remote.py

Playwright Server can be prepared very easily with Docker like this:

FROM mcr.microsoft.com/playwright:focal

WORKDIR /root
RUN npm install playwright@1.12.3 && ./node_modules/.bin/playwright install
CMD ["./node_modules/.bin/playwright", "run-server"]
Enter fullscreen mode Exit fullscreen mode

Now we can enjoy playwright-python on Alpine:

from playwright_remote.sync_api import sync_playwright_remote

with sync_playwright_remote('ws://127.0.0.1:8080/ws') as playwright:
  browser = playwright.chromium.launch()
  try:
    page = browser.new_page()
    page.goto('https://github.com/YusukeIwaki')
    page.screenshot(path='YusukeIwaki.png')
  finally:
    browser.close()
Enter fullscreen mode Exit fullscreen mode

I also tried it on Heroku: https://zenn.dev/yusukeiwaki/scraps/3ad5f8ed536720 (Sorry, Japanese only...)

Conclusion

playwright-python is really awesome, it runs on Alpine actually.

Note that WebSocketTransport (introduced in this article) is internal class and can be changed in future releases.

Just expect the feature is officially supported. Let's upvote
https://github.com/microsoft/playwright/issues/4687 đź‘Ť

Top comments (0)