Vision

Connect to browser

Introduction

To perform actions in the browser by means of code written in some programming language, you need to use third-party libraries such as Puppeteer, Playwright, Selenium and others.

It is important to note that at the moment it is possible to connect for automation purposes only to those profiles that have been started via API.

You can read about how to start and stop profiles via API in the corresponding documentation.

Start and stop profiles

Methods to start and stop profiles by API

In this document you can find documentation on how to connect to a profile through one of the above libraries using several programming languages.

Puppeteer

General information

Puppeteer is a Node.js library that allows you to automate Chromium-based browser processes using Chrome DevTools Protocol.

In other words, you can automate your actions in your browser profile: opening tabs, walking around the site, entering data into forms, and so on.

Below we've added some examples of working with the profile via Puppeteer in JavaScript (Node.js) and Python (Pyppeteer). Each example includes comments for your convenience.

Instructions on how to add libraries to your project can be found on the developer's website.

Examples

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.connect({
    browserURL: 'http://127.0.0.1:20701',
    defaultViewport: null,
});

const context = browser.browserContexts()[0];
const page = await context.newPage();

await page.goto('https://google.com');

await page.close();
browser.disconnect();
})();
import asyncio
from pyppeteer import connect

async def main():
    browser = await connect(browserURL='http://127.0.0.1:20701', defaultViewport=None)

    page = await browser.newPage()
    await page.goto('https://www.google.com')

    await page.close()
    await browser.disconnect()

asyncio.get_event_loop().run_until_complete(main())

Playwright

General information

Playwright is a Node.js library for automating Chromium, Firefox and WebKit using APIs.

It is not inferior to Puppeteer and other automation libraries in terms of functionality.

Examples

const { chromium } = require('playwright');

(async () => {
    const browser = await chromium.connectOverCDP('http://127.0.0.1:19397');
    const context = browser.contexts()[0];
    // Create a new page in the context.
    const tab = await context.newPage();
    // Navigate to a URL.
    await tab.goto('https://www.google.com', {waitUntil: 'domcontentloaded'});
    // Close the tab.
    await tab.close();
    await browser.close();
})();
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.connect_over_cdp(endpoint_url='http://127.0.0.1:20701')
        context = browser.contexts[0]

        page = await context.new_page()
        await page.goto('https://www.google.com')

        await page.close()


asyncio.get_event_loop().run_until_complete(main())
using Microsoft.Playwright;

using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.ConnectOverCDPAsync("http://127.0.0.1:16591");

var context = browser.Contexts[0];
var page = await context.NewPageAsync();

await page.GotoAsync("https://www.google.com", new PageGotoOptions() { WaitUntil = WaitUntilState.NetworkIdle });

var title = await page.TitleAsync();
Console.WriteLine(title);

Chromiumoxide (Rust)

General information

Automation using the Rust programming language is accomplished through the Chromiumoxide library

Examples

use futures_util::StreamExt;

//// Cargo.toml
// [dependencies]
// chromiumoxide = { version = "0.5.6", default-features = false, features = [
//     "tokio",
//     "tokio-runtime",
// ] }
// futures-util = "0.3.29"
// tokio = { version = "1.34.0", default-features = false, features = [
//     "rt-multi-thread",
//     "macros",
// ] }

#[tokio::main]
async fn main() {
// Connect to a browser instance
let (client, mut handler) = chromiumoxide::Browser::connect("http://127.0.0.1:20701")
    .await
    .expect("failed to connect to browser");

// Spawn a thread with the handler
tokio::spawn(async move {
    loop {
        let _ = handler.next().await.expect("failed to handle");
        }
    });

    // Create a new tab
let tab = client
    .new_page("about:blank")
    .await
    .expect("failed to create tab");

// Navigate to a URL
tab.goto("https://www.google.com")
    .await
    .expect("failed to navigate");

// Print the title of the page
let title = tab.get_title().await.expect("failed to get title");
println!("title: {:?}", title);

// Close the tab and disconnect
tab.close().await.expect("failed to close tab");
}

Chromedp (Golang)

General information

Automation using the Go programming language is done through the chromedp library

Chromedp is a quick and easy way to manage Chrome DevTools Protocol-enabled browsers in Go without external dependencies.

Examples

package main

import (
"context"

"github.com/chromedp/chromedp"
)

func main() {
ctx, cancel := chromedp.NewRemoteAllocator(context.Background(), "http://127.0.0.1:17986")
defer cancel()

ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

// Open google.com.
if err := chromedp.Run(ctx,
    chromedp.Navigate("https://www.google.com/"),
); err != nil {
    panic(err)
}
}

On this page