#web-driver #automation #geckodriver #chromedriver #web-page #phantomjs


High-level API for programmatically interacting with web pages through WebDriver

67 releases

0.20.0-rc.7 Sep 16, 2023
0.20.0-rc.4 Jan 7, 2023
0.20.0-rc.3 Nov 7, 2022
0.19.2 Jul 30, 2022
0.7.1 Jul 9, 2017

#16 in HTTP client

Download history 5038/week @ 2024-01-01 6303/week @ 2024-01-08 6826/week @ 2024-01-15 6189/week @ 2024-01-22 5996/week @ 2024-01-29 6196/week @ 2024-02-05 8118/week @ 2024-02-12 9524/week @ 2024-02-19 10893/week @ 2024-02-26 10078/week @ 2024-03-04 9728/week @ 2024-03-11 8282/week @ 2024-03-18 6297/week @ 2024-03-25 7158/week @ 2024-04-01 5733/week @ 2024-04-08 5687/week @ 2024-04-15

25,530 downloads per month
Used in 40 crates (25 directly)


3.5K SLoC


Crates.io Documentation codecov Gitter chat

A high-level API for programmatically interacting with web pages through WebDriver.

This crate uses the WebDriver protocol to drive a conforming (potentially headless) browser through relatively high-level operations such as "click this element", "submit this form", etc.

Most interactions are driven by using CSS selectors. With most WebDriver-compatible browser being fairly recent, the more expressive levels of the CSS standard are also supported, giving fairly powerful operators.

Forms are managed by first calling Client::form, and then using the methods on Form to manipulate the form's fields and eventually submitting it.

For low-level access to the page, Client::source can be used to fetch the full page HTML source code, and Client::raw_client_for to build a raw HTTP request for a particular URL.


These examples all assume that you have a WebDriver compatible process running on port 4444. A quick way to get one is to run geckodriver at the command line.

Let's start out clicking around on Wikipedia:

use fantoccini::{ClientBuilder, Locator};

// let's set up the sequence of steps we want the browser to take
async fn main() -> Result<(), fantoccini::error::CmdError> {
    let c = ClientBuilder::native().connect("http://localhost:4444").await.expect("failed to connect to WebDriver");

    // first, go to the Wikipedia page for Foobar
    let url = c.current_url().await?;
    assert_eq!(url.as_ref(), "https://en.wikipedia.org/wiki/Foobar");

    // click "Foo (disambiguation)"

    // click "Foo Lake"
    c.find(Locator::LinkText("Foo Lake")).await?.click().await?;

    let url = c.current_url().await?;
    assert_eq!(url.as_ref(), "https://en.wikipedia.org/wiki/Foo_Lake");


How did we get to the Foobar page in the first place? We did a search! Let's make the program do that for us instead:

// -- snip wrapper code --
// go to the Wikipedia frontpage this time
// find the search form, fill it out, and submit it
let f = c.form(Locator::Css("#search-form")).await?;
f.set_by_name("search", "foobar").await?

// we should now have ended up in the right place
let url = c.current_url().await?;
assert_eq!(url.as_ref(), "https://en.wikipedia.org/wiki/Foobar");

// -- snip wrapper code --

What if we want to download a raw file? Fantoccini has you covered:

// -- snip wrapper code --
// go back to the frontpage
// find the source for the Wikipedia globe
let img = c.find(Locator::Css("img.central-featured-logo")).await?;
let src = img.attr("src").await?.expect("image should have a src");
// now build a raw HTTP client request (which also has all current cookies)
let raw = img.client().raw_client_for(fantoccini::Method::GET, &src).await?;

// we then read out the image bytes
use futures_util::TryStreamExt;
let pixels = raw
    .try_fold(Vec::new(), |mut data, chunk| async move {
// and voilà, we now have the bytes for the Wikipedia logo!
assert!(pixels.len() > 0);
println!("Wikipedia logo is {}b", pixels.len());

// -- snip wrapper code --

For more examples, take a look at the examples/ directory.

Contributing to fantoccini

The following information applies only to developers interested in contributing to this project. If you simply want to use it to automate web browsers you can skip this section.

How to run tests

The tests assume that you have chromedriver and geckodriver already running on your system. You can download them using the links above. Then run them from separate tabs in your terminal. They will stay running until terminated with Ctrl+C or until the terminal session is closed.

Then run cargo test from this project directory.


~344K SLoC