Creating a Live Playwright REPL for Browser Automation over SSH or API
Playwright is already one of the best tools for browser automation — but what if you could connect to a browser session remotely and script it live? This article shows you how to build a Node.js REPL interface connected to a running Playwright browser, allowing live control via terminal, SSH, or even WebSockets. Use cases include: Debugging headless sessions on remote servers Teaching or pairing in live browser contexts Automated QA with human override Controlled automation bots (via API or CLI) Step 1: Set Up a Headless Browser with Playwright Start by launching a Playwright browser that stays open: npm install playwright const { chromium } = require('playwright'); (async () => { const browser = await chromium.launch({ headless: false }); const context = await browser.newContext(); const page = await context.newPage(); await page.goto('https://example.com'); // Keep alive global.browser = browser; global.page = page; })(); Step 2: Start a Node.js REPL That Has Access to page Let’s expose the Playwright page to a live REPL: const repl = require('repl'); const r = repl.start({ prompt: 'playwright> ', useGlobal: true }); r.context.page = global.page; Now anything you type into this REPL — like await page.screenshot() — runs directly in the open browser session. Step 3: Allow Remote Access Over SSH By running this script on a remote server, you can SSH in and attach to the live REPL: ssh user@host node remote-playwright.js Once connected, you can do: playwright> await page.click('text=Login') playwright> await page.fill('#email', 'me@example.com') It’s like a programmable Chrome DevTools, from the CLI. Step 4: Optional – Expose REPL Over WebSocket or API For advanced cases, expose this REPL interface over WebSocket using libraries like ws or socket.io: const WebSocket = require('ws'); const { Writable } = require('stream'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', (ws) => { const stream = new Writable({ write(chunk, _, cb) { ws.send(chunk.toString()); cb(); } }); const r = repl.start({ input: ws, output: stream, prompt: 'webrepl> ', terminal: false, useGlobal: true }); r.context.page = global.page; }); Now you can connect live to the REPL from a web client or external system. Step 5: Bonus – Add Sandbox or Timeout Protections To make your live REPL safer, add timeouts or sandbox restrictions using tools like vm2 or use setTimeout to auto-disconnect idle users. const { NodeVM } = require('vm2'); const vm = new NodeVM({ console: 'inherit', sandbox: { page: global.page }, timeout: 3000 }); vm.run('await page.screenshot()'); ✅ Pros:
Playwright is already one of the best tools for browser automation — but what if you could connect to a browser session remotely and script it live?
This article shows you how to build a Node.js REPL interface connected to a running Playwright browser, allowing live control via terminal, SSH, or even WebSockets.
Use cases include:
- Debugging headless sessions on remote servers
- Teaching or pairing in live browser contexts
- Automated QA with human override
- Controlled automation bots (via API or CLI)
Step 1: Set Up a Headless Browser with Playwright
Start by launching a Playwright browser that stays open:
npm install playwright
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch({ headless: false });
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://example.com');
// Keep alive
global.browser = browser;
global.page = page;
})();
Step 2: Start a Node.js REPL That Has Access to page
Let’s expose the Playwright page to a live REPL:
const repl = require('repl');
const r = repl.start({
prompt: 'playwright> ',
useGlobal: true
});
r.context.page = global.page;
Now anything you type into this REPL — like await page.screenshot()
— runs directly in the open browser session.
Step 3: Allow Remote Access Over SSH
By running this script on a remote server, you can SSH in and attach to the live REPL:
ssh user@host
node remote-playwright.js
Once connected, you can do:
playwright> await page.click('text=Login')
playwright> await page.fill('#email', 'me@example.com')
It’s like a programmable Chrome DevTools, from the CLI.
Step 4: Optional – Expose REPL Over WebSocket or API
For advanced cases, expose this REPL interface over WebSocket using libraries like ws
or socket.io
:
const WebSocket = require('ws');
const { Writable } = require('stream');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', (ws) => {
const stream = new Writable({
write(chunk, _, cb) {
ws.send(chunk.toString());
cb();
}
});
const r = repl.start({
input: ws,
output: stream,
prompt: 'webrepl> ',
terminal: false,
useGlobal: true
});
r.context.page = global.page;
});
Now you can connect live to the REPL from a web client or external system.
Step 5: Bonus – Add Sandbox or Timeout Protections
To make your live REPL safer, add timeouts or sandbox restrictions using tools like vm2
or use setTimeout
to auto-disconnect idle users.
const { NodeVM } = require('vm2');
const vm = new NodeVM({
console: 'inherit',
sandbox: { page: global.page },
timeout: 3000
});
vm.run('await page.screenshot()');
✅ Pros: