Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there an easy way to set cookie? #754

Closed
hsluoyz opened this issue Feb 22, 2021 · 5 comments
Closed

Is there an easy way to set cookie? #754

hsluoyz opened this issue Feb 22, 2021 · 5 comments

Comments

@hsluoyz
Copy link

hsluoyz commented Feb 22, 2021

I saw the example here: https://github.com/chromedp/examples/blob/master/cookie/main.go , but it looks a little too complicated. For example, I don't know what a cookieServer is used for.

Moreover, the setcookies() function seems to do something like "read the returned values". In my case, I already know what cookie I need to set as follows, including cookie name, value, domain and all other properties (by logging in the site manually). I just need to set these static cookie info into ChromeDP.

image

I want to have a simple API like:

err := chromedp.Run(ctx,
		chromedp.SetCookie(cookie), // key1 = value1
		chromedp.Navigate(url),

		chromedp.Sleep(1*time.Second),
		chromedp.SendKeys(`//*[@id="username"]`, username),
		chromedp.SendKeys(`//*[@id="loginform"]/div[3]/input`, password),
		chromedp.Click(`//*[@id="btn-login"]`),
	)
	if err != nil {
		panic(err)
	}

Is it possible?

@ZekeLu
Copy link
Member

ZekeLu commented Feb 22, 2021

The example illustrates many things, that's why it looks "complicated":

  1. It sets browser cookies (L70-L85);
  2. It reads browser cookies (L90-L102).
  3. It even provides a simple web server to illustrate that the provided cookies are sent to the server ( cookieServer);

The source code in part 1 is what you need.

BTW, I think it's a bad idea to wait like this: chromedp.Sleep(1*time.Second). Try to use one of the following actions:

  • chromedp.WaitReady
  • chromedp.WaitVisible
  • chromedp.WaitEnabled
  • and other query actions

@hsluoyz
Copy link
Author

hsluoyz commented Feb 22, 2021

Thanks!

@hsluoyz hsluoyz closed this as completed Feb 22, 2021
@hzmsrv
Copy link

hzmsrv commented Apr 27, 2022

but the document.cookie is null

@ZekeLu
Copy link
Member

ZekeLu commented Apr 27, 2022

@hzmnet Please provide a minimal demo to show what's wrong.

@hzmsrv
Copy link

hzmsrv commented Apr 28, 2022

@ZekeLu please check to run the code document.cookie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants