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

[Bug]: Cookies not set in headless mode #31736

Closed
develorer opened this issue Jul 17, 2024 · 5 comments
Closed

[Bug]: Cookies not set in headless mode #31736

develorer opened this issue Jul 17, 2024 · 5 comments

Comments

@develorer
Copy link

develorer commented Jul 17, 2024

Version

1.45.0

Steps to reproduce

  1. run the following python script:
from playwright.sync_api import sync_playwright
sync_playwright_started = sync_playwright().start()
binary_path = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
data_dir = SPECIFY_YOUR_OWN_DATA_DIR_FROM_YOUR_DAILY_BROWSER
context = sync_playwright_started.chromium.launch_persistent_context(headless=True,
                                                                     executable_path=binary_path,
                                                                     user_data_dir=data_dir,
                                                                     ignore_default_args=['--use-mock-keychain'])
print(context.cookies())
context.close()
  1. Notice this prints an empty list. No cookies are set

  2. Run the exact same script, but flip headless from True to False

  3. Notice this prints a list full of cookies. Cookies are set

  4. If I do not use ignore_default_args=['--use-mock-keychain'] then cookies are never set, whether headless or not

Expected behavior

I expect that cookies are set in headless mode.

Actual behavior

No cookies are set in headless mode, when a user_data_dir is specified.

Additional context

No response

Environment

System: MacOS
@dgozman
Copy link
Contributor

dgozman commented Jul 18, 2024

@develorer You should first navigate to a page that will set some cookies. I don't see that in your snippet.

Given the above, I suspect that I misunderstood the issue. Could you please explain in more details what cookies do you expect to see?

@develorer
Copy link
Author

develorer commented Jul 18, 2024

The user-data-dir's Default/Cookies was populated manually, by launching the browser, and browsing without automation. I have tried to make that a little clearer in the initial post.

@dgozman
Copy link
Contributor

dgozman commented Jul 22, 2024

@develorer Thank you for more details. I think that to achieve interoperability, you should always run the browser with the --use-mock-keychain flag. So, launch the browser with this flag manually to populate the user data dir, and then let Playwright reuse the same user data dir later, where it will add this flag automatically. Let me know whether this helps.

@develorer
Copy link
Author

@dgozman thanks for your thoughts. That didn't work out on my end. However I found a workaround:

if I use --headless=new instead of the playwright's default --headless then it works fine and processes cookies correctly.

I am not sure why, but in any case I don't mind using the new headless mode. According to chromium's documentation, it is closer to the real thing. I am aware that it is slower, but you can't have everything in life 😉

@pavelfeldman
Copy link
Member

Closing as per above, please feel free to open a new issue if this does not cover your use case.

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