diff --git a/src/crawlee/browsers/_playwright_browser.py b/src/crawlee/browsers/_playwright_browser.py index 8ce19bfd26..2616e124fb 100644 --- a/src/crawlee/browsers/_playwright_browser.py +++ b/src/crawlee/browsers/_playwright_browser.py @@ -17,6 +17,8 @@ logger = getLogger(__name__) +_UNSUPPORTED_PERSISTENT_CONTEXT_OPTIONS = frozenset({'storage_state'}) + @docs_group('Browser management') class PlaywrightPersistentBrowser(Browser): @@ -59,6 +61,14 @@ async def new_context(self, **context_options: Any) -> BrowserContext: if self._context: raise RuntimeError('Persistent browser can have only one context') + unsupported_options = _UNSUPPORTED_PERSISTENT_CONTEXT_OPTIONS.intersection(context_options) + if unsupported_options: + unsupported_list = ', '.join(sorted(unsupported_options)) + raise ValueError( + 'The following browser_new_context_options are not supported when using a persistent browser ' + f'context: {unsupported_list}. Use `use_incognito_pages=True` if you need these options.' + ) + launch_options = self._browser_launch_options | context_options if self._user_data_dir: diff --git a/tests/unit/browsers/test_playwright_browser.py b/tests/unit/browsers/test_playwright_browser.py index 120b886c59..94e6a14333 100644 --- a/tests/unit/browsers/test_playwright_browser.py +++ b/tests/unit/browsers/test_playwright_browser.py @@ -2,6 +2,7 @@ from pathlib import Path from typing import TYPE_CHECKING +from unittest.mock import AsyncMock import pytest from playwright.async_api import async_playwright @@ -42,3 +43,13 @@ async def test_delete_temp_folder_with_close_browser(playwright: Playwright) -> assert current_temp_dir.exists() await persist_browser.close() assert not current_temp_dir.exists() + + +async def test_new_context_rejects_storage_state_for_persistent_browser() -> None: + mocked_browser_type = AsyncMock() + persist_browser = PlaywrightPersistentBrowser(mocked_browser_type, user_data_dir=None, browser_launch_options={}) + + with pytest.raises(ValueError, match='storage_state'): + await persist_browser.new_context(storage_state={'cookies': []}) + + mocked_browser_type.launch_persistent_context.assert_not_awaited()