Crate headless_chrome
source ·Expand description
A high-level API to control headless Chrome or Chromium over the DevTools Protocol. It is the Rust equivalent of Puppeteer, a Node library maintained by the Chrome DevTools team.
It is not 100% feature compatible with Puppeteer, but there’s enough here to satisfy most browser testing / web crawling use cases, and there are several ‘advanced’ features such as:
- network request interception
- JavaScript coverage monitoring
- taking screenshots of elements or the entire page
- saving pages to PDF
- ‘headful’ browsing
- automatic downloading of ‘known good’ Chromium binaries for Linux / Mac / Windows
- extension pre-loading
Quick Start
use headless_chrome::{Browser, protocol::page::ScreenshotFormat};
use headless_chrome::protocol::cdp::Page;
fn browse_wikipedia() -> Result<(), failure::Error> {
let browser = Browser::default()?;
let tab = browser.wait_for_initial_tab()?;
/// Navigate to wikipedia
tab.navigate_to("https://www.wikipedia.org")?;
/// Wait for network/javascript/dom to make the search-box available
/// and click it.
tab.wait_for_element("input#searchInput")?.click()?;
/// Type in a query and press `Enter`
tab.type_str("WebKit")?.press_key("Enter")?;
/// We should end up on the WebKit-page once navigated
tab.wait_for_element("#firstHeading")?;
assert!(tab.get_url().ends_with("WebKit"));
/// Take a screenshot of the entire browser window
let _jpeg_data = tab.capture_screenshot(
Page::CaptureScreenshotFormatOption::Png,
Some(75),
None,
true)?;
/// Take a screenshot of just the WebKit-Infobox
let _png_data = tab
.wait_for_element("#mw-content-text > div > table.infobox.vevent")?
.capture_screenshot(ScreenshotFormat::PNG)?;
Ok(())
}
assert!(browse_wikipedia().is_ok());
Re-exports
Modules
Structs
Represents the way in which Chrome is run. By default it will search for a Chrome
binary on the system, use an available port for debugging, and start in headless mode.
Builder for
LaunchOptions
.