Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

re-add support for cookies_file param

Nick Sweetingf5bb3828a022
diff --git a/browser_use/browser/profile.py b/browser_use/browser/profile.py
index 6fbafb62c..e27b3e4dc 100644
--- a/browser_use/browser/profile.py
+++ b/browser_use/browser/profile.py
@@ -590,7 +590,9 @@ class BrowserProfile(BrowserConnectArgs, BrowserLaunchPersistentContextArgs, Bro
save_har_path: str | None = Field(default=None, description='Directory for saving HAR files.')
trace_path: str | None = Field(default=None, description='Directory for saving trace files.')
- cookies_file: str | None = Field(default=None, description='File to save cookies to.')
+ cookies_file: str | None = Field(
+ default=None, description='File to save cookies to. DEPRECATED, use `storage_state` instead.'
+ )
# extension_ids_to_preinstall: list[str] = Field(
# default_factory=list, description='List of Chrome extension IDs to preinstall.'
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index a8bf7babe..afb9e9576 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -516,6 +516,9 @@ class BrowserSession(BaseModel):
f'Failed to create a playwright BrowserContext {self.browser_context} for browser={self.browser}'
)
+ # Load cookies from file if specified
+ await self.load_cookies_from_file()
+
# async def _fork_locked_user_data_dir(self) -> None:
# """Fork an in-use user_data_dir by cloning it to a new location to allow a second browser to use it"""
# # TODO: implement copy-on-write using overlayfs or zfs or something
@@ -1049,6 +1052,33 @@ class BrowserSession(BaseModel):
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(cookies, indent=4)) # TODO: replace with anyio asyncio or anyio write
+ async def load_cookies_from_file(self) -> None:
+ """
+ Load cookies from the cookies_file if it exists and apply them to the browser context.
+ """
+ if not self.browser_profile.cookies_file or not self.browser_context:
+ return
+
+ # Show deprecation warning
+ logger.warning(
+ '⚠️ cookies_file is deprecated and will be removed in a future version. '
+ 'Please use storage_state instead for loading cookies and other browser state. '
+ 'See: https://playwright.dev/python/docs/api/class-browsercontext#browser-context-storage-state'
+ )
+
+ cookies_path = Path(self.browser_profile.cookies_file)
+ if (not cookies_path.is_absolute()) and not str(cookies_path).startswith('.'):
+ cookies_path = Path(self.browser_profile.downloads_dir or '.') / cookies_path
+
+ if cookies_path.exists():
+ try:
+ cookies_data = json.loads(cookies_path.read_text())
+ if cookies_data:
+ await self.browser_context.add_cookies(cookies_data)
+ logger.info(f'🍪 Loaded {len(cookies_data)} cookies from {_log_pretty_path(cookies_path)}')
+ except Exception as e:
+ logger.warning(f'❌ Failed to load cookies from {_log_pretty_path(cookies_path)}: {type(e).__name__}: {e}')
+
# @property
# def browser_extension_pages(self) -> list[Page]:
# if not self.browser_context:
diff --git a/tests/ci/test_browser_session_cookies.py b/tests/ci/test_browser_session_cookies.py
new file mode 100644
index 000000000..53390f019
--- /dev/null
+++ b/tests/ci/test_browser_session_cookies.py
@@ -0,0 +1,217 @@
+"""
+Test script for BrowserSession cookie functionality.
+
+Tests cover:
+- Loading cookies from cookies_file on browser start
+- Saving cookies to cookies_file
+- Verifying cookies are applied to browser context
+"""
+
+import json
+import logging
+import tempfile
+from pathlib import Path
+
+import pytest
+from pytest_httpserver import HTTPServer
+
+from browser_use.browser.profile import BrowserProfile
+from browser_use.browser.session import BrowserSession
+
+# Set up test logging
+logger = logging.getLogger('browser_session_cookie_tests')
+
+
+class TestBrowserSessionCookies:
+ """Tests for BrowserSession cookie loading and saving functionality."""
+
+ @pytest.fixture
+ async def temp_cookies_file(self):
+ """Create a temporary cookies file with test cookies."""
+ with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
+ test_cookies = [
+ {
+ 'name': 'test_cookie',
+ 'value': 'test_value',
+ 'domain': 'localhost',
+ 'path': '/',
+ 'expires': -1,
+ 'httpOnly': False,
+ 'secure': False,
+ 'sameSite': 'Lax',
+ },
+ {
+ 'name': 'session_cookie',
+ 'value': 'session_12345',
+ 'domain': 'localhost',
+ 'path': '/',
+ 'expires': -1,
+ 'httpOnly': True,
+ 'secure': False,
+ 'sameSite': 'Lax',
+ },
+ ]
+ json.dump(test_cookies, f)
+ temp_path = Path(f.name)
+
+ yield temp_path
+
+ # Cleanup
+ temp_path.unlink(missing_ok=True)
+
+ @pytest.fixture
+ async def browser_profile_with_cookies(self, temp_cookies_file):
+ """Create a BrowserProfile with cookies_file set."""
+ profile = BrowserProfile(headless=True, user_data_dir=None, cookies_file=str(temp_cookies_file))
+ yield profile
+
+ @pytest.fixture
+ async def browser_session_with_cookies(self, browser_profile_with_cookies):
+ """Create a BrowserSession with cookie file configured."""
+ session = BrowserSession(browser_profile=browser_profile_with_cookies)
+ yield session
+ # Cleanup
+ try:
+ await session.stop()
+ except Exception:
+ pass
+
+ @pytest.fixture
+ def http_server(self, httpserver: HTTPServer):
+ """Set up HTTP server with test endpoints."""
+ # Endpoint that shows cookies
+ httpserver.expect_request('/cookies').respond_with_data(
+ """
+ <html>
+ <body>
+ <h1>Cookie Test Page</h1>
+ <script>
+ document.write('<p>Cookies: ' + document.cookie + '</p>');
+ </script>
+ </body>
+ </html>
+ """,
+ content_type='text/html',
+ )
+ return httpserver
+
+ async def test_cookies_loaded_on_start(self, browser_session_with_cookies, http_server):
+ """Test that cookies are loaded from cookies_file when browser starts."""
+ # Start the browser session
+ await browser_session_with_cookies.start()
+
+ # Verify cookies were loaded
+ cookies = await browser_session_with_cookies.get_cookies()
+ assert len(cookies) >= 2, 'Expected at least 2 cookies to be loaded'
+
+ # Check specific cookies
+ cookie_names = {cookie['name'] for cookie in cookies}
+ assert 'test_cookie' in cookie_names
+ assert 'session_cookie' in cookie_names
+
+ # Verify cookie values
+ test_cookie = next(c for c in cookies if c['name'] == 'test_cookie')
+ assert test_cookie['value'] == 'test_value'
+ assert test_cookie['domain'] == 'localhost'
+
+ async def test_cookies_available_in_page(self, browser_session_with_cookies, http_server):
+ """Test that loaded cookies are available to web pages."""
+ # Start the browser session
+ await browser_session_with_cookies.start()
+
+ # Navigate to test page
+ page = await browser_session_with_cookies.get_current_page()
+ await page.goto(http_server.url_for('/cookies'))
+
+ # Check that cookies are available to the page
+ page_cookies = await page.evaluate('document.cookie')
+ assert 'test_cookie=test_value' in page_cookies
+
+ async def test_save_cookies(self, browser_profile_with_cookies, temp_cookies_file):
+ """Test saving cookies to file."""
+ # Create a new temp file for saving
+ save_path = temp_cookies_file.parent / 'saved_cookies.json'
+
+ session = BrowserSession(browser_profile=browser_profile_with_cookies)
+ await session.start()
+
+ # Navigate to a page and set a new cookie
+ page = await session.get_current_page()
+ await page.goto('about:blank')
+ await page.context.add_cookies([{'name': 'new_cookie', 'value': 'new_value', 'domain': 'localhost', 'path': '/'}])
+
+ # Save cookies
+ await session.save_cookies(save_path)
+
+ # Verify saved file exists and contains cookies
+ assert save_path.exists()
+ saved_cookies = json.loads(save_path.read_text())
+ assert len(saved_cookies) >= 3 # Original 2 + 1 new
+
+ cookie_names = {cookie['name'] for cookie in saved_cookies}
+ assert 'new_cookie' in cookie_names
+
+ # Cleanup
+ save_path.unlink(missing_ok=True)
+ await session.stop()
+
+ async def test_nonexistent_cookies_file(self):
+ """Test that browser starts normally when cookies_file doesn't exist."""
+ # Use a non-existent file path
+ profile = BrowserProfile(headless=True, user_data_dir=None, cookies_file='/tmp/nonexistent_cookies.json')
+
+ session = BrowserSession(browser_profile=profile)
+ # Should start without errors
+ await session.start()
+
+ # Should have no cookies
+ cookies = await session.get_cookies()
+ assert len(cookies) == 0
+
+ await session.stop()
+
+ async def test_invalid_cookies_file(self, tmp_path):
+ """Test that browser handles invalid cookie file gracefully."""
+ # Create a file with invalid JSON
+ invalid_file = tmp_path / 'invalid_cookies.json'
+ invalid_file.write_text('not valid json')
+
+ profile = BrowserProfile(headless=True, user_data_dir=None, cookies_file=str(invalid_file))
+
+ session = BrowserSession(browser_profile=profile)
+ # Should start without errors (warning logged)
+ await session.start()
+
+ # Should have no cookies
+ cookies = await session.get_cookies()
+ assert len(cookies) == 0
+
+ await session.stop()
+
+ async def test_relative_cookies_file_path(self, browser_profile_with_cookies):
+ """Test that relative cookies_file paths work correctly."""
+ # Create profile with relative path
+ profile = BrowserProfile(
+ headless=True,
+ user_data_dir=None,
+ cookies_file='test_cookies.json', # Relative path
+ downloads_dir=browser_profile_with_cookies.downloads_dir,
+ )
+
+ # Copy test cookies to expected location
+ expected_path = Path(profile.downloads_dir) / 'test_cookies.json'
+ expected_path.parent.mkdir(parents=True, exist_ok=True)
+ expected_path.write_text(
+ json.dumps([{'name': 'relative_cookie', 'value': 'relative_value', 'domain': 'localhost', 'path': '/'}])
+ )
+
+ session = BrowserSession(browser_profile=profile)
+ await session.start()
+
+ cookies = await session.get_cookies()
+ cookie_names = {cookie['name'] for cookie in cookies}
+ assert 'relative_cookie' in cookie_names
+
+ # Cleanup
+ expected_path.unlink(missing_ok=True)
+ await session.stop()