Obtain HttpOnly=True Session Cookies from Services Logged in via Browser-Use

Python
2025-05-21 12:10 (2 days ago) ytyng

Here is how you can obtain session cookies issued when logging into a web service using browser-use.

Session cookies are typically issued with httponly=true, so even if you request browser-use to "retrieve session cookies," they won't be returned. This is because they cannot be accessed via JavaScript.

If you want to obtain them, you can prepare a browser_use.Browser instance in advance and inject it into the browser-use Agent, allowing you to handle the Playwright browser instance during processing.

This approach is effective not only for session cookies but also when dealing with finer resources inside the browser.

from browser_use import Agent, Browser
from langchain_openai import ChatOpenAI

DEFAULT_LLM_MODEL = "gpt-4.1-mini"

def get_session_cookies():

    browser = Browser()
    current_cookies = []

    async def handle_step(browser_state, agent_output, step_number):
        nonlocal browser, current_cookies
        try:
            # Handle the Playwright instance
            context = browser.playwright_browser.contexts[0]
            current_cookies = list(await context.cookies())
        except Exception as e:
            print(
                'An error occurred while getting cookies: '
                f'{e.__class__.__name__}: {e}'
            )

    _login_prompt = 'Please log into our web service ◯◯.\n\n...'

    async def run_agent():
        agent = Agent(
            task=_login_prompt,
            browser=browser,
            llm=ChatOpenAI(model=DEFAULT_LLM_MODEL),
            register_new_step_callback=handle_step,
        )
        await agent.run(max_steps=5)

    asyncio.run(run_agent())

    # Login complete. Cookies should be obtained here
    print(current_cookies)
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Archive

2025
2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011