jonhoo / fantoccini

A high-level API for programmatically interacting with web pages through WebDriver.
Apache License 2.0
1.68k stars 125 forks source link

SessionNotCreated error on second run with fantoccini and geckodriver #206

Closed jhiver closed 1 year ago

jhiver commented 1 year ago

Hello & thanks for your library!

I have the following issue with this code:

use fantoccini::ClientBuilder;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Launch a Firefox browser instance using WebDriver
    let client = ClientBuilder::native()
        .connect("http://localhost:4444")
        .await?;

    // Navigate to a website
    client.goto("https://google.com/").await?;

    Ok(())
}

When running the code multiple times, it results in a SessionNotCreated error with the following message:

Error: SessionNotCreated(WebDriver { error: SessionNotCreated, message: "Session is already started", stacktrace: "" })

I have to kill/restart geckodriver to be able to run the code again.

Any ideas how to fix it?

Also, it would be really handy to have some code in the README that shows how to save / reuse session/profile information such as cookies.

Thanks a lot!

jonhoo commented 1 year ago

Ah, yes, this is because Firefox's geckodriver doesn't allow concurrent sessions, combined with the fact that you're not calling and then awaiting Client::close which would cleanly terminate the previous session before the next one is started. See the paragraph in the documentation on Client. Adding a client.close.await? before you return Ok() should fix the issue :)

For higher-level operation, I recommend you use something like @stevepryde's thirtyfour, which will probably make it easier to do things like keep state over time.