pub struct Browser { /* private fields */ }
Expand description

A handle to an instance of Chrome / Chromium, which wraps a WebSocket connection to its debugging port.

Most of your actual “driving” (e.g. clicking, typing, navigating) will be via instances of Tab, which are accessible via methods such as get_tabs.

A Browser can either manage its own Chrome process or connect to a remote one.

Browser::default().unwrap() will return a headless instance of whatever browser can be found using default_executable, which will search on your PATH for relevant binaries or use the path specified in the CHROME env var.

You can use LaunchOptions to automatically download a revision of Chromium that has a compatible API into your $XDG_DATA_DIR. Alternatively, you can specify your own path to a binary, or make use of the default_executable function to use your already-installed copy of Chrome.

Option 1: Managing a Chrome process

use headless_chrome::Browser;
let browser = Browser::default()?;
let first_tab = browser.new_tab()?;
assert_eq!("about:blank", first_tab.get_url());

Option 2: Connecting to a remote Chrome service

  • see /examples/print_to_pdf.rs for a working example

While the Chrome DevTools Protocol (CDTP) does define some methods in a “Browser” domain (such as for resizing the window in non-headless mode), we currently don’t implement those.

Implementations§

source§

impl Browser

source

pub fn new(launch_options: LaunchOptions<'_>) -> Result<Self>

Launch a new Chrome browser.

The browser will have its user data (aka “profile”) directory stored in a temporary directory. The browser process will be killed when this struct is dropped.

source

pub fn default() -> Result<Self>

Calls Browser::new with options to launch a headless browser using whatever Chrome / Chromium binary can be found on the system.

source

pub fn connect(debug_ws_url: String) -> Result<Self>

Allows you to drive an externally-launched Chrome process instead of launch one via Browser::new. If the browser is idle for 30 seconds, the connection will be dropped.

source

pub fn connect_with_timeout( debug_ws_url: String, idle_browser_timeout: Duration ) -> Result<Self>

Allows you to drive an externally-launched Chrome process instead of launch one via Browser::new. If the browser is idle for idle_browser_timeout, the connection will be dropped.

source

pub fn get_process_id(&self) -> Option<u32>

source

pub fn get_tabs(&self) -> &Arc<Mutex<Vec<Arc<Tab>>>>

The tabs are behind an Arc and Mutex because they’re accessible from multiple threads (including the one that handles incoming protocol events about new or changed tabs).

source

pub fn wait_for_initial_tab(&self) -> Result<Arc<Tab>>

👎Deprecated since 1.0.4: Use new_tab() instead.

Chrome always launches with at least one tab. The reason we have to ‘wait’ is because information about that tab isn’t available immediately after starting the process. Tabs are behind Arcs because they each have their own thread which handles events and method responses directed to them.

Wait timeout: 10 secs

source

pub fn new_tab(&self) -> Result<Arc<Tab>>

Create a new tab and return a handle to it.

If you want to specify its starting options, see new_tab_with_options.

let first_tab = browser.new_tab()?;
let new_tab = browser.new_tab()?;
let num_tabs = browser.get_tabs().lock().unwrap().len();
assert_eq!(2, num_tabs);
source

pub fn new_tab_with_options( &self, create_target_params: CreateTarget ) -> Result<Arc<Tab>>

Create a new tab with a starting url, height / width, context ID and ‘frame control’

   let new_tab = browser.new_tab_with_options(CreateTarget {
   url: "chrome://version",
   width: Some(1024),
   height: Some(800),
   browser_context_id: None,
   enable_begin_frame_control: None,
   })?;
source

pub fn new_context(&self) -> Result<Context<'_>>

Creates the equivalent of a new incognito window, AKA a browser context

source

pub fn get_version(&self) -> Result<GetVersionReturnObject>

Get version information

let version_info = browser.get_version()?;
println!("User-Agent is `{}`", version_info.user_agent);

Trait Implementations§

source§

impl Clone for Browser

source§

fn clone(&self) -> Browser

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V