Enables us to execute playwright-python scripts on Pure-Python environment.
pip install git+https://github.com/YusukeIwaki/playwright-python-remote
For communicating with Playwright server:
from playwright_remote.sync_api import sync_playwright_remote
with sync_playwright_remote('ws://127.0.0.1:8080/ws') as playwright:
with playwright.chromium.launch() as browser:
page = browser.new_page()
page.goto('https://github.com/YusukeIwaki')
page.screenshot(path='YusukeIwaki.png')
Just replace sync_playwright
with sync_playwright_remote('ws://xxxxxxx/ws')
.
For communicating with Playwright Browser server:
from playwright_remote.sync_api import connect_to_browser
with connect_to_browser('ws://127.0.0.1:xxxxx/xxxxxxxxxxxxxxxxxxxxxx') as browser:
page = browser.new_page()
page.goto('https://github.com/YusukeIwaki')
page.screenshot(path='YusukeIwaki.png')
We have to prepare Playwright server using playwright CLI.
In local development environment (Node.js is required), just execute:
npx [email protected] install && npx [email protected] run-server
For deploying to PaaS servers, we can use Playwright official Docker image: https://hub.docker.com/_/microsoft-playwright
FROM mcr.microsoft.com/playwright:v1.20.0
WORKDIR /root
RUN npm install [email protected] && ./node_modules/.bin/playwright install
CMD ["./node_modules/.bin/playwright", "run-server"]
Heroku example can be found here.
We can also share only one browser environment via WebSocket.
npx [email protected] install && npx [email protected] launch-server --browser firefox
Note that the functionality of CLI for launch-server is not so rich. For sharing more detailed environment, we have to prepare JavaScript server code like below:
const playwright = require('playwright')
// https://playwright.dev/docs/api/class-browsertype/#browser-type-launch-server
option = {
channel: 'chrome-canary',
headless: false,
port: 8080,
}
playwright.chromium.launchServer(option).then((server) => { console.log(server.wsEndpoint()) })
Since playwright-remote works on Pure-Python environment, it works also on Alpine Linux.
Unfortunately, pip install playwright
cannot be executed on Alpine. We have to install playwright from git at this moment.
FROM python:3.9-alpine
RUN apk add --no-cache --virtual .install-deps build-base curl git \
&& pip install git+https://github.com/microsoft/[email protected] \
&& pip install git+https://github.com/YusukeIwaki/playwright-python-remote \
&& apk del .install-deps
Now, we can enjoy Playwright on the Docker image :)
Note that WebSocket endpoint URL should be set properly to sync_playwright_remote("ws://xxxxxxxxx/ws")
.