Git Product home page Git Product logo

playwright-python-remote's Introduction

playwright-remote

Enables us to execute playwright-python scripts on Pure-Python environment.

image

Setup

pip install git+https://github.com/YusukeIwaki/playwright-python-remote

Example

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')

Launch Playwright server

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.18.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.

Launch Playwright Browser server

We can also share only one browser environment via WebSocket.

npx [email protected] install && npx [email protected] launch-server 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()) })

Execute Python script on Alpine Linux environment

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").

playwright-python-remote's People

Contributors

yusukeiwaki avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.