Libraries
::: warning Notice
Docker is standalone with Nstbrowser dashboard, please do not confuse the APIs between them.
:::
Websocket API
- Connect to browser:
ws:127.0.0.1:8848/ws/connect
Connect to or launch a browser instance, query parameters:
Name | Kind | Type | Reqiured | Description |
---|---|---|---|---|
profileId | query | string | False | the profileId to connect to, a browser will be launched with random config if not passed |
config | query | string | False | launch config, in encoded json string, you can custom launch config if profileId not passed,the full config refer to LaunchNewBrowser |
You can use Puppeteer, Playwright, Chromedp or another CDP library to connect our websocket API.
JavaScript Puppeteer
Puppeteer is a Node.js library which provides a high-level API to control Nstchrome over the DevTools Protocol.
// javascript puppeteer: npm install puppeteer
const puppeteer = require("puppeteer");
(async () => {
const host = "127.0.0.1:8848";
const browserWSEndpoint = `ws://${host}/ws/connect`;
try {
const browser = await puppeteer.connect({
browserWSEndpoint: browserWSEndpoint,
});
const page = await browser.newPage();
await page.goto("https://google.com");
await page.screenshot({path: "screenshot.png", fullPage: true});
// ...
await browser.close();
} catch (err) {
// ...
}
})();
JavaScript Playwright CDP
Playwright is a framework for Web Testing and Automation. It allows testing Nstchrome with a single API.
// javascript playwright CDP: npm install playwright
import {chromium} from "playwright";
(async () => {
const host = "127.0.0.1:8848";
const browserWSEndpoint = `ws://${host}/ws/connect`;
let browser = await chromium.connectOverCDP(browserWSEndpoint);
const context = await browser.newContext();
const page = await context.newPage();
await page.goto("https://www.google.com/");
await page.screenshot({path: "screenshot.png"});
// ...
})();
Python Pyppeteer
Pyppeteer is a headless chrome/chromium automation library for Python and requires python version >= 3.8
import asyncio
from pyppeteer import launcher
async def main():
browser = await launcher.connect(
browserWSEndpoint='ws://127.0.0.1:8848/ws/connect'
)
page = await browser.newPage()
await page.goto('https://google.com')
await page.screenshot({'path': 'screenshot.png'})
await browser.close()
asyncio.get_event_loop().run_until_complete(main())
Golang Chromedp
Package chromedp is a faster, simpler way to drive browsers supporting the Chrome DevTools Protocol in Go without external dependencies.
package main
import (
"context"
"flag"
"log"
"os"
"github.com/chromedp/chromedp"
)
func main() {
devtoolsWsURL := flag.String("devtools-ws-url", "ws://127.0.0.1:8848/ws/connect", "DevTools WebSsocket URL")
flag.Parse()
allocatorContext, cancel := chromedp.NewRemoteAllocator(context.Background(), *devtoolsWsURL, chromedp.NoModifyURL)
defer cancel()
ctx, cancel := chromedp.NewContext(allocatorContext)
defer cancel()
var buf []byte
// capture entire browser viewport, returning png with quality=90
if err := chromedp.Run(ctx, fullScreenshot(`https://google.com/`, 90, &buf)); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("screenshot.png", buf, 0o644); err != nil {
log.Fatal(err)
}
}
// fullScreenshot takes a screenshot of the entire browser viewport.
//
// Note: chromedp.FullScreenshot overrides the device's emulation settings. Use
// device.Reset to reset the emulation and viewport settings.
func fullScreenshot(urlstr string, quality int, res *[]byte) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.FullScreenshot(res, quality),
}
}
Last modified: 3 months ago