go-rod / rod

A Chrome DevTools Protocol driver for web automation and scraping.
https://go-rod.github.io
MIT License
5.19k stars 341 forks source link

Get cookies json unmarshal error `page.MustCookies()` #1094

Open fakis opened 1 month ago

fakis commented 1 month ago

Rod Version: v0.116.2

The code to demonstrate your question

  1. Try to open youtube.com with a browser
  2. Log in with Google authorization
  3. An error occurred while getting the cookies for the youtube.com page

    func TestYoutubePageGetCookies(t *testing.T) {
    u := launcher.NewUserMode().Headless(false).MustLaunch()
    browser := rod.New().ControlURL(u).MustConnect().NoDefaultDevice()
    defer browser.MustClose()
    
    page := browser.MustPage("https://www.youtube.com/")
    
    // During the waiting time, log in with Google authorization
    time.Sleep(30 * time.Second)
    
    // After authorization, get page cookies and report an error.
    cookies := page.MustCookies()
    fmt.Println(cookies)
    }

Error details

panic: json: cannot unmarshal string into Go struct field NetworkCookie.cookies.partitionKey of type proto.NetworkCookiePartitionKey [recovered]
    panic: json: cannot unmarshal string into Go struct field NetworkCookie.cookies.partitionKey of type proto.NetworkCookiePartitionKey

goroutine 34 [running]:
testing.tRunner.func1.2({0x8429a0, 0xc000428140})
    C:/Users/Administrator/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.3.windows-amd64/src/testing/testing.go:1631 +0x24a
testing.tRunner.func1()
    C:/Users/Administrator/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.3.windows-amd64/src/testing/testing.go:1634 +0x377
panic({0x8429a0?, 0xc000428140?})
    C:/Users/Administrator/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.3.windows-amd64/src/runtime/panic.go:770 +0x132
github.com/go-rod/rod/lib/utils.init.func2({0x8429a0?, 0xc000428140?})
    C:/Users/Administrator/go/pkg/mod/github.com/go-rod/rod@v0.116.2/lib/utils/utils.go:68 +0x1d
yt.TestYoutubePageGetCookies.New.(*Browser).WithPanic.genE.func1({0xc00008e690?, 0x0?, 0x372eda?})
    C:/Users/Administrator/go/pkg/mod/github.com/go-rod/rod@v0.116.2/must.go:36 +0x62
github.com/go-rod/rod.(*Page).MustCookies(0xc00042a000, {0x0?, 0x35ac3f?, 0xc84ad0?})
    C:/Users/Administrator/go/pkg/mod/github.com/go-rod/rod@v0.116.2/must.go:197 +0x97
yt.TestYoutubePageGetCookies(0xc0003a2340?)
    D:/www/rpa/yt/rod_test.go:25 +0x470
testing.tRunner(0xc0003a2340, 0xac4da8)
    C:/Users/Administrator/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.3.windows-amd64/src/testing/testing.go:1689 +0xfb
created by testing.(*T).Run in goroutine 1
    C:/Users/Administrator/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.3.windows-amd64/src/testing/testing.go:1742 +0x390

type NetworkCookie struct { ...

// PartitionKey (experimental) (optional) Cookie partition key.
PartitionKey *NetworkCookiePartitionKey `json:"partitionKey,omitempty"` 

}

github-actions[bot] commented 1 month ago

Please fix the format of your markdown:

7 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "3. An error occurred while get..."]
8 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```go"]
26 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```"]
26 MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"]
51 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "- Is it possible that the `Par..."]
52 MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```go"]

Please fix the golang code in your markdown:

@@ golang markdown block 1 @@
4:2: expected '}', found '...'
7:2: expected ';', found PartitionKey

generated by check-issue

Fly-Playgroud commented 1 month ago

Colud you please show your browser type and version

ysmood commented 1 month ago

It's because the latest cdp protocol has changed the data fields. If you upgrade your chrome to the latest version, the problem should be resolved. Or you can use a old version of rod to use the old protocol.

Fly-Playgroud commented 1 month ago

1097

ysmood commented 1 month ago

FYI https://go-rod.github.io/#/compatibility?id=browser-and-cdp-protocol-versioning

fakis commented 1 month ago

ok, thank you, it is indeed such a problem, but the field such as cdp can also be changed

It's because the latest cdp protocol has changed the data fields. If you upgrade your chrome to the latest version, the problem should be resolved. Or you can use a old version of rod to use the old protocol.