-
-
Notifications
You must be signed in to change notification settings - Fork 201
Open
Labels
p2-bugSomething isn't workingSomething isn't working
Description
Environments
- playwright-go Version: [e.g. v0.5200.0]
- Browser: Chromium or Firefox
- OS and version: Windows 11
Bug description
The StorageState()method fails to capture cookies. When attempting to save authentication cookies using context.StorageState(cookiePath), the resulting file contains empty cookies arrays ("cookies": []) despite successful authentication visible in the browser session.
To Reproduce
Please provide a mini reproduction rather than just a description. For example:
package poster
import (
"ams/config"
"ams/logger"
"github.com/playwright-community/playwright-go"
"go.uber.org/zap"
)
type XhsPoster struct {
cfg *config.Config
}
func NewXhsPoster(cfg *config.Config) *XhsPoster {
return &XhsPoster{cfg: cfg}
}
func (p *XhsPoster) login(statusChan chan *LoginStatus, cookiePath string) error {
pw, err := playwright.Run()
if err != nil {
return err
}
browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
Headless: playwright.Bool(false),
})
if err != nil {
return err
}
context, err := browser.NewContext(playwright.BrowserNewContextOptions{
Viewport: &playwright.Size{
Width: 1920,
Height: 1080,
},
})
if err != nil {
return err
}
if err := context.AddInitScript(playwright.Script{
Path: &p.cfg.Server.StealthScript,
}); err != nil {
return err
}
page, err := browser.NewPage()
if err != nil {
return err
}
_, err = page.Goto("https://creator.xiaohongshu.com/")
if err != nil {
return err
}
// 登录
if err := page.Locator("img.css-wemwzq").Click(); err != nil {
return err
}
// 获取二维码
src, err := page.GetByRole("img").Nth(2).GetAttribute("src")
if err != nil {
return err
}
statusChan <- &LoginStatus{
Code: 200,
Data: src,
Type: "qrcode",
}
if err := page.WaitForURL("https://creator.xiaohongshu.com/new/home"); err != nil {
return err
}
logger.Info("登录成功")
if rsp, err := context.StorageState(cookiePath); err != nil {
return err
} else {
logger.Info("StorageState", zap.Any("cookie", rsp))
}
if err = browser.Close(); err != nil {
logger.Error("could not close browser", zap.Error(err))
}
if err = pw.Stop(); err != nil {
logger.Error("could not stop Playwright", zap.Error(err))
}
return nil
}
func (p *XhsPoster) Login(statusChan chan *LoginStatus, cookiePath string) error {
if err := p.login(statusChan, cookiePath); err != nil {
logger.Error("login failed", zap.Error(err))
statusChan <- &LoginStatus{
Code: 500,
Data: err.Error(),
Type: "close",
}
return err
} else {
statusChan <- &LoginStatus{
Code: 200,
Data: "Login success",
Type: "close",
}
return nil
}
}
Metadata
Metadata
Assignees
Labels
p2-bugSomething isn't workingSomething isn't working