microsoft / playwright-python

Python version of the Playwright testing and automation library.
https://playwright.dev/python/
Apache License 2.0
11.5k stars 875 forks source link

[Bug]: cookie not load in browser.new_context(storage_state=cookies_path) #2529

Closed iridesc closed 1 week ago

iridesc commented 3 weeks ago

Version

1.45.1

Steps to reproduce

  1. init a browser
  2. create a cookie json file with content like
    [
    {
    "name": "ABC",
    "value": "1111",
    "domain": ".test.com",
    "path": "/",
    "expires": 1755849293.606346,
    "httpOnly": false,
    "secure": false,
    "sameSite": "Lax"
    }
    ]

Expected behavior

use code like :

        self.context = self.browser.new_context(
            storage_state=self.cookies_path
        )

        with open(self.cookies_path, "r") as f:
            print(f"cookie: {f.read()}")

        print(f"{self.context.cookies()=}")

can print:

cookie: [
  {
    "name": "ABC",
    "value": "1111",
    "domain": ".test.com",
    "path": "/",
    "expires": 1755849293.606346,
    "httpOnly": false,
    "secure": false,
    "sameSite": "Lax"
  }
]
self.context.cookies()=[{'name': 'ABC', 'value': '1111', 'domain': '.test.com', 'path': '/', 'expires': 1755849293.606346, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}]
[{'name': 'ABC', 'value': '1111', 'domain': '.test.com', 'path': '/', 'expires': 1755849293.606346, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}]

Actual behavior

output:

cookie: [
  {
    "name": "ABC",
    "value": "1111",
    "domain": ".test.com",
    "path": "/",
    "expires": 1755849293.606346,
    "httpOnly": false,
    "secure": false,
    "sameSite": "Lax"
  }
]
self.context.cookies()=[]

Additional context

the cookie provided by storage_state in new_context not load. but if i use add_cookies method worked fine: code:

        self.context = self.browser.new_context(
            storage_state=self.cookies_path
        )

        with open(self.cookies_path, "r") as f:
            file_content = f.read()
            print(f"cookie: {file_content}")
            self.context.add_cookies(json.loads(file_content))

        print(f"{self.context.cookies()=}")

output:

cookie: [
  {
    "name": "ABC",
    "value": "1111",
    "domain": ".test.com",
    "path": "/",
    "expires": 1755849293.606346,
    "httpOnly": false,
    "secure": false,
    "sameSite": "Lax"
  }
]
self.context.cookies()=[{'name': 'ABC', 'value': '1111', 'domain': '.test.com', 'path': '/', 'expires': 1755849293.606346, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}]
[{'name': 'ABC', 'value': '1111', 'domain': '.test.com', 'path': '/', 'expires': 1755849293.606346, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}]

Environment

- Operating System: [manjaro]
- CPU: [amd64]
- Browser: [Chromium, Firefox]
- Python Version: [3.12]
mxschmitt commented 2 weeks ago

The data structures you are using seem not aligning to what Playwright expects, hence its not adding any cookies. Have you tried the following?

{
    "cookies": [
        {
          "name": "ABC",
          "value": "1111",
          "domain": ".test.com",
          "path": "/",
          "expires": 1755849293.606346,
          "httpOnly": false,
          "secure": false,
          "sameSite": "Lax"
        }
      ]
}
mxschmitt commented 1 week ago

Closing as part of the triage process since it seemed stale. Please create a new issue with a detailed reproducible or feature request if you still face issues.