How to use Cookies method of rod Package

Best Rod code snippet using rod.Cookies

dvcscraper.go

Source:dvcscraper.go Github

copy

Full Screen

...64 err = fmt.Errorf("failed to connect to browser: %w", err)65 return scraper, err66 }67 if !opts.SkipSession {68 err = scraper.readCookies()69 if err != nil {70 err = fmt.Errorf("failed to read cookies: %w", err)71 }72 }73 return scraper, err74}75func (s *Scraper) readCookies() error {76 _, err := os.Stat(cookieSessionFile)77 if os.IsNotExist(err) {78 // no previous session; continue79 return nil80 } else if err != nil {81 err = fmt.Errorf("failed to check for session file: %w", err)82 return err83 }84 cookieReader, err := os.Open(cookieSessionFile)85 if err != nil {86 err = fmt.Errorf("failed to read cookie session file: %w", err)87 return err88 }89 defer cookieReader.Close()90 err = s.SetCookies(cookieReader)91 if err != nil {92 err = fmt.Errorf("failed to set cookies: %w", err)93 return err94 }95 return nil96}97// GetCookies returns a JSON encoded set of the current Scraper's browser's cookies.98//99// This is used in conjunction with `SetCookies` to pre- or re-load browser100// state across scraper runs. For example, to bypass logging in each run and reuse101// sessions.102//103// Callers of `GetCookies` should persist the returned bytes and then call104// `SetCookies` later with the same content. This will "resume" where you left off.105func (s *Scraper) GetCookies() ([]byte, error) {106 cookies, err := s.browser.GetCookies()107 if err != nil {108 err = fmt.Errorf("failed to get cookies from browser: %w", err)109 return []byte{}, err110 }111 raw, err := json.Marshal(&cookies)112 if err != nil {113 err = fmt.Errorf("failed to marshal cookies: %w", err)114 return []byte{}, err115 }116 return raw, nil117}118// SetCookies takes JSON content from the provided io.reader and sets cookies119// on the Scraper's browser.120//121// This is used in conjunction with `GetCookies` to carry-over the current122// browser state/session forward to subsequent scraper runs.123func (s *Scraper) SetCookies(raw io.Reader) error {124 buf := bytes.Buffer{}125 _, err := buf.ReadFrom(raw)126 if err != nil {127 err = fmt.Errorf("failed to read cookie reader: %w", err)128 return err129 }130 cookies := []*proto.NetworkCookie{}131 err = json.Unmarshal(buf.Bytes(), &cookies)132 if err != nil {133 err = fmt.Errorf("failed to unmarshal cookies: %w", err)134 return err135 }136 s.browser.SetCookies(proto.CookiesToParams(cookies))137 return nil138}139// Close cleans up resources for the Scraper140func (s *Scraper) Close() error {141 err := s.cleanup()142 if err != nil {143 s.logger.Println(err.Error())144 }145 return s.browser.Close()146}147func (s *Scraper) cleanup() error {148 cookieWriter, err := os.Create(cookieSessionFile)149 if err != nil {150 err = fmt.Errorf("failed to open session cookie file: %w", err)151 return err152 }153 defer cookieWriter.Close()154 cookieBytes, err := s.GetCookies()155 if err != nil {156 err = fmt.Errorf("failed to get cookies: %w", err)157 return err158 }159 _, err = cookieWriter.Write(cookieBytes)160 return err161}162func (s *Scraper) Screenshot(filename string) error {163 page, err := s.getPage()164 if err != nil {165 err = fmt.Errorf("failed to get page for screenshot: %w", err)166 return err167 }168 page.MustScreenshotFullPage(filename)...

Full Screen

Full Screen

cookie.go

Source:cookie.go Github

copy

Full Screen

...5 "time"6 "github.com/go-rod/rod"7 "github.com/go-rod/rod/lib/proto"8)9type RodCookies []*proto.NetworkCookie10func (r RodCookies) ToHttpCookies() (cookies []*http.Cookie) {11 cookies = make([]*http.Cookie, 0, len(r))12 for _, obj := range r {13 cookies = append(cookies, &http.Cookie{14 Name: obj.Name,15 Value: obj.Value,16 Path: obj.Path,17 Domain: obj.Domain,18 Expires: obj.Expires.Time(),19 Secure: obj.Secure,20 HttpOnly: obj.HTTPOnly,21 SameSite: 0,22 Raw: "",23 Unparsed: nil,24 })25 }26 return27}28func GetHttpCookies(page *rod.Page) (cookies []*http.Cookie, err error) {29 var rodCookies []*proto.NetworkCookie30 rodCookies, err = GetCookies(page)31 if err != nil {32 err = fmt.Errorf("GetCookies: %w", err)33 return34 }35 cookies = RodCookies(rodCookies).ToHttpCookies()36 return37}38func GetCookies(page *rod.Page) (cookies []*proto.NetworkCookie, err error) {39 cookies, err = page.Timeout(time.Second * 3).Cookies([]string{40 "https://e.qq.com",41 "https://sso.e.qq.com",42 "https://mp.weixin.qq.com",43 "https://ad.qq.com",44 })45 return46}47func GetCookieMap(page *rod.Page) (cookieMap map[string]*proto.NetworkCookie, err error) {48 cookies, err := GetCookies(page)49 if err != nil {50 err = fmt.Errorf("get cookies: %w", err)51 return52 }53 cookieMap = make(map[string]*proto.NetworkCookie)54 for _, cookie := range cookies {55 cookieMap[cookie.Name] = cookie56 }57 return58}59func GetCookie(page *rod.Page, name string) (cookie *proto.NetworkCookie, err error) {60 var found bool61 var cookieMap map[string]*proto.NetworkCookie62 cookieMap, err = GetCookieMap(page)...

Full Screen

Full Screen

browser.go

Source:browser.go Github

copy

Full Screen

...13 defer p.Close()14 var cookies []*proto.NetworkCookie15 sessionFound := false16 for !sessionFound {17 cookies = p.MustCookies()18 for _, v := range cookies {19 if v.Name == "session" {20 sessionFound = true21 break22 }23 }24 }25 return &LoginData{26 Cookies: cookies,27 }, nil28}...

Full Screen

Full Screen

Cookies

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 MustLaunch()4 defer l.Close()5 browser := rod.New().ControlURL(l).MustConnect()6 defer browser.MustClose()7 cookies := page.MustCookies()8 for _, c := range cookies {9 log.Println(c.Name, c.Value)10 }11}

Full Screen

Full Screen

Cookies

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 cookies := page.MustCookies()5 fmt.Println(cookies)6}7[{Name:1P_JAR Domain:www.google.com Path:/ Expires:2021-07-18 21:35:42.376 +0530 IST HTTPOnly:false Secure:false SameSite:} {Name:NID Domain:www.google.com Path:/ Expires:2022-07-18 21:35:42.376 +0530 IST HTTPOnly:false Secure:false SameSite:} {Name:CONSENT Domain:www.google.com Path:/ Expires:2021-07-18 21:35:42.376 +0530 IST HTTPOnly:false Secure:false SameSite:}]

Full Screen

Full Screen

Cookies

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 l := launcher.New().Bin("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe").Launch()4 defer l.Close()5 browser := rod.New().ControlURL(l).MustConnect()6 defer browser.MustClose()7 fmt.Println(page.MustCookies())8}9[{"name":"SID","value":"...","domain":".google.com","path":"/","expires":1640906881,"size":67,"httpOnly":true,"secure":true,"session":false},{"name":"HSID","value":"...","domain":".google.com","path":"/","expires":1640906881,"size":67,"httpOnly":true,"secure":true,"session":false},{"name":"SSID","value":"...","domain":".google.com","path":"/","expires":1640906881,"size":67,"httpOnly":true,"secure":true,"session":false},{"name":"APISID","value":"...","domain":".google.com","path":"/","expires":1640906881,"size":67,"httpOnly":true,"secure":true,"session":false},{"name":"SAPISID","value":"...","domain":".google.com","path":"/","expires":1640906881,"size":67,"httpOnly":true,"secure":true,"session":false},{"name":"NID","value":"...","domain":".google.com","path":"/","expires":1640906881,"size":67,"httpOnly":true,"secure":true,"session":false},{"name":"1P_JAR","value":"...","domain":".google.com","path":"/","expires":1640906881,"size":67,"httpOnly":true,"secure":true,"session":false},{"name":"OGPC","value":"...","domain":".google.com","path":"/","expires":1640906881,"size":67,"httpOnly":true,"secure":true,"session":false},{"name":"CONSENT","value":"...","domain":".google.com","path":"/","expires":1640906881,"size":67,"httpOnly":true,"secure":true,"session":false},{"name":"ANID","value":"...","domain":".google

Full Screen

Full Screen

Cookies

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 fmt.Println(page.Cookies())5}6[{Name:1P_JAR Value:2021-06-28-08 SameSite: Domain:google.com Path:/ Expires:2021-08-27 08:00:00 +0000 UTC HttpOnly:false Secure:false} {Name:CONSENT Value:WP.284b6d SameSite: Domain:google.com Path:/ Expires:2022-06-27 08:00:00 +0000 UTC HttpOnly:false Secure:false} {Name:NID Value:217=ZnYg8W SameSite: Domain:google.com Path:/ Expires:2022-07-02 08:00:00 +0000 UTC HttpOnly:false Secure:false} {Name:OGPC Value:19142414- SameSite: Domain:google.com Path:/ Expires:2022-06-27 08:00:00 +0000 UTC HttpOnly:false Secure:false} {Name:SID Value: SameSite: Domain:google.com Path:/ Expires:2021-06-28 08:00:00 +0000 UTC HttpOnly:false Secure:false} {Name:SIDCC Value: SameSite: Domain:google.com Path:/ Expires:2021-06-28 08:

Full Screen

Full Screen

Cookies

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 cookies, err := page.Cookies()5 if err != nil {6 fmt.Println(err)7 }8 for _, cookie := range cookies {9 fmt.Println(cookie)10 }11 browser.Close()12}

Full Screen

Full Screen

Cookies

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(e.Text())4 })5}6import (7func main() {8 fmt.Println(e.Text())9 })10}11import (12func main() {13 fmt.Println(e.Text())14 })15}16import (17func main() {18 fmt.Println(e.Text())19 })20}21import (22func main() {23 fmt.Println(e.Text())24 })25}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Rod automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful