Getting Started with Rust Headless Chrome

This article will walk you through the process of leveraging BrowserCat and Rust Headless Chrome for browser automation at scale.

Prerequisites

Before you get started, you’ll need to:

  1. Sign up for a free BrowserCat account.
  2. Create an API key.
  3. Install Rust Headless Chrome in your project…
cargo add headless-chrome

Your first automation

The following script connects to a BrowserCat browser, navigate to a website, and print the page title.

With a little tweaking (and the Rust Headless Chrome docs), you can automate literally anything you can do in a real browser.

use headless_chrome::{Browser, LaunchOptions, protocol::cdp::Page};
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let bcat_url = "wss://api.browsercat.com/connect?apiKey=<YOUR_API_KEY>";

    let browser = Browser::connect(bcat_url)?;

    // Do some stuff...
    let tab = browser.new_tab()?;
    tab.navigate_to("https://example.com")?;
    tab.wait_for_element("body")?;

    let title = tab.evaluate("document.title", false)?
        .value()
        .and_then(|v| v.as_str().map(String::from))
        .ok_or("Failed to get title")?;

    println!("{}", title);

    Ok(())
}

Configure the browser

It’s really easy to configure our browsers for your needs. Whether you want to connect to a specific region, set up a third-party proxy, or customize your browser launch args, we’ve got you covered.

See out browser config docs for more info.