Thank you Terry. This definitely solves my first problem of connecting to Browser instance.
However, it seems there are slight differences between the selenium’s get_cookie and add_cookie & browsers get_cookie and add_cookie.
For example, using selenium driver:
add_cookie(cookie)
when cookie = {‘domain’: ‘.my-test-domain.com’, ‘expiry’: 1775920102, ‘name’: ‘_ids’, ‘path’: ‘/’, ‘sameSite’: ‘None’, ‘secure’: True, ‘value’: ‘my-value’}
- python code successful, no exception thrown
&
get_cookie(name)
when name = _ids_legacy
- python code successful, no exception thrown: returns:
data={“value”:{“domain”:“.my-test-domain.com”,“expiry”:1775920102,“httpOnly”:false,“name”:“_ids_legacy”,“path”:“/”,“sameSite”:“Lax”,“secure”:true,“value”:“my-value”}} | headers=HTTPHeaderDict({‘Content-Length’: ‘264’, ‘Content-Type’: ‘application/json; charset=utf-8’, ‘cache-control’: ‘no-cache’})
However, when driver is browser instance,
add_cookie(cookie)
when cookie = {‘domain’: ‘.my-test-domain.com’, ‘expiry’: 1775920102, ‘name’: ‘_ids’, ‘path’: ‘/’, ‘sameSite’: ‘None’, ‘secure’: True, ‘value’: ‘my-value’}
- python error: TypeError: Cookie.add_cookie() missing 1 required positional argument: ‘value’
&
get_cookie(name)
when name = _ids_legacy
- python TypeError: Object of type datetime is not JSON serializable
returns:
‘_ids_legacy’: {‘name’: ‘_ids_legacy’, ‘value’: ‘ATkOy-2ja2-1XU_glJJcUHxd5S9UiQZ0mHqCVxaZ55zqwo72ovr6doZnq9erqMPMmYJytYI8hsf3c53MkB5Z5IX4PERxfRI4B7fkKg’, ‘domain’: ‘.auth-us1.smarttech-prod.com’, ‘path’: ‘/’, ‘expires’: datetime.datetime(2026, 4, 11, 15, 13, 17, 736, tzinfo=datetime.timezone.utc), ‘httpOnly’: False, ‘secure’: True, ‘sameSite’: ‘Lax’}
So final solution would be to update my custom add_cookie function to convert cookie dict to a individual string parameters, before passing to driver.add_cookie()
and to convert the datatime to epoch before passing to driver.get_cookie().
But, first I will have a look at the actual selenium and browser python functions to be sure this is format expected.