How to fix: Chromedriver Page Immediately Closes

638 Views Asked by At

I run a program that tries to open a webpage using agouti/chromedriver in go, but as soon as it calls the new page, it closes the page. Subsequent attempts to access the page result in an "invalid session id" error. How can I get the page to stay open?

This is using Go (version 1.13.3 darwin/amd64) on Mac (version 10.14.5), with chromedriver (version 76.0.3809.68). I have the latest version of Google Chrome installed as well. I have tried updating chrome and agouti to no improved results. I have quit and restarted Chrome, uninstalled and reinstalled Chrome, and restarted my computer, none of which have worked. I have run with several chromedriver options (seen below), and run without them. I have tried with (seen below) and without time.Sleep() between functions. My regular chrome browser works fine.

package main

import (
    "fmt"
    "time"

    "github.com/sclevine/agouti"
)

func main() {
    driver := agouti.ChromeDriver(
        agouti.ChromeOptions("args", []string{
            "--headless",
            "--no-sandbox",
            "--disable-dev-shm-usage",
            "--disable-gpu",
            "--whitelisted-ips",
            "--detach",
        }),
        agouti.Debug,
    )

    fmt.Println("start")
    err := driver.Start()
    if err != nil {
        fmt.Println("Error starting driver: " + err.Error())
        return
    }

    page, err := driver.NewPage(agouti.Browser("chrome"))
    if err != nil {
        fmt.Println("Error creating new page: " + err.Error())
        return
    }

    time.Sleep(1 * time.Second)

    err = page.Navigate("https://www.google.com")
    if err != nil {
        fmt.Println("Error navigating to job post link: " + err.Error())
        return
    }

    time.Sleep(1 * time.Second)
    fmt.Println("end")

}

When not using the --headless tag, I can see the browser window open and close in less than a second.

Expected output:

start
Starting ChromeDriver 76.0.3809.68 (420c9498db8ce8fcd190a954d51297672c1515d5-refs/branch-heads/3809@{#864}) on port 53489
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
end

Actual Output:

start
Starting ChromeDriver 76.0.3809.68 (420c9498db8ce8fcd190a954d51297672c1515d5-refs/branch-heads/3809@{#864}) on port 53489
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Error navigating to job post link: failed to navigate: request unsuccessful: invalid session id
1

There are 1 best solutions below

1
On BEST ANSWER

I found the solution; turns out I needed to reinstall chromedriver. Looking back through my logs, the issue occurred due to Chrome automatically updating overnight, and no longer working with my previous version of chromedriver.