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.

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?


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


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.

This library provides a Playwright initializer (ContextManager) just replacing PipeTransport with WebSocketTransport.

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


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://') as playwright:
  browser = playwright.chromium.launch()
    page = browser.new_page()
Enter fullscreen mode Exit fullscreen mode

I also tried it on Heroku: (Sorry, Japanese only...)


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 đź‘Ť

Top comments (0)