playwright-python
playwright-python copied to clipboard
[Bug]: cookie not load in browser.new_context(storage_state=cookies_path)
Version
1.45.1
Steps to reproduce
- init a browser
- 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]
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"
}
]
}
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.