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 (opens in a new tab), Playwright (opens in a new tab), Selenium (opens in a new tab) 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 (opens in a new tab) in JavaScript (Node.js) and Python (Pyppeteer (opens in a new tab)). 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

1const puppeteer = require('puppeteer');
2
3(async () => {
4    const browser = await puppeteer.connect({
5    browserURL: 'http://localhost:20701',
6    defaultViewport: null,
7});
8
9const context = browser.browserContexts()[0];
10const page = await context.newPage();
11
12await page.goto('https://google.com');
13
14await page.close();
15browser.disconnect();
16})();

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

1const { chromium } = require('playwright');
2
3(async () => {
4    const browser = await chromium.connectOverCDP('http://localhost:19397');
5    const context = browser.contexts()[0];
6    // Create a new page in the context.
7    const tab = await context.newPage();
8    // Navigate to a URL.
9    await tab.goto('https://www.google.com', {waitUntil: 'domcontentloaded'});
10    // Close the tab.
11    await tab.close();
12    await browser.close();
13})();
1using Microsoft.Playwright;
2
3using var playwright = await Playwright.CreateAsync();
4await using var browser = await playwright.Chromium.ConnectOverCDPAsync("http://localhost:16591");
5
6var context = browser.Contexts[0];
7var page = await context.NewPageAsync();
8
9await page.GotoAsync("https://www.google.com", new PageGotoOptions() { WaitUntil = WaitUntilState.NetworkIdle });
10
11var title = await page.TitleAsync();
12Console.WriteLine(title);

Chromiumoxide (Rust)

General information

Automation using the Rust programming language is accomplished through the Chromiumoxide (opens in a new tab) library

Examples

1use futures_util::StreamExt;
2
3//// Cargo.toml
4// [dependencies]
5// chromiumoxide = { version = "0.5.6", default-features = false, features = [
6//     "tokio",
7//     "tokio-runtime",
8// ] }
9// futures-util = "0.3.29"
10// tokio = { version = "1.34.0", default-features = false, features = [
11//     "rt-multi-thread",
12//     "macros",
13// ] }
14
15#[tokio::main]
16async fn main() {
17    // Connect to a browser instance
18    let (client, mut handler) = chromiumoxide::Browser::connect("http://localhost:20701")
19        .await
20        .expect("failed to connect to browser");
21
22    // Spawn a thread with the handler
23    tokio::spawn(async move {
24        loop {
25            let _ = handler.next().await.expect("failed to handle");
26            }
27        });
28
29        // Create a new tab
30    let tab = client
31        .new_page("about:blank")
32        .await
33        .expect("failed to create tab");
34
35    // Navigate to a URL
36    tab.goto("https://www.google.com")
37        .await
38        .expect("failed to navigate");
39
40    // Print the title of the page
41    let title = tab.get_title().await.expect("failed to get title");
42    println!("title: {:?}", title);
43
44    // Close the tab and disconnect
45    tab.close().await.expect("failed to close tab");
46}
47

Chromedp (Golang)

General information

Automation using the Go programming language is done through the chromedp (opens in a new tab) library

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

Examples

1package main
2
3import (
4    "context"
5
6    "github.com/chromedp/chromedp"
7    )
8
9func main() {
10    ctx, cancel := chromedp.NewRemoteAllocator(context.Background(), "http://localhost:17986")
11    defer cancel()
12
13    ctx, cancel = chromedp.NewContext(ctx)
14    defer cancel()
15
16    // Open google.com.
17    if err := chromedp.Run(ctx,
18        chromedp.Navigate("https://www.google.com/"),
19    ); err != nil {
20        panic(err)
21    }
22}