microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
65.4k stars 3.56k forks source link

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

Closed develorer closed 1 month ago

develorer commented 1 month ago

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 commented 1 month ago

@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 commented 1 month ago

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 commented 1 month ago

@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 commented 1 month ago

@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 commented 1 month ago

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