Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/crawlee/browsers/_playwright_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

logger = getLogger(__name__)

_UNSUPPORTED_PERSISTENT_CONTEXT_OPTIONS = frozenset({'storage_state'})


@docs_group('Browser management')
class PlaywrightPersistentBrowser(Browser):
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/browsers/test_playwright_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Loading