Fix: use target_id to find correct page in get_page

This commit is contained in:
unclecode
2025-12-13 06:51:54 +00:00
parent b2e4a1f2e3
commit c1e485e0b0

View File

@@ -1184,29 +1184,33 @@ class BrowserManager:
await self._apply_stealth_to_page(page) await self._apply_stealth_to_page(page)
else: else:
context = self.default_context context = self.default_context
pages = context.pages
page = next((p for p in pages if p.url == crawlerRunConfig.url), None) # When target_id is provided, use it to find the specific page
if not page: # This is critical for concurrent requests sharing a browser
if pages: if self.config.browser_context_id and self.config.target_id:
page = pages[0] page = await self._get_page_by_target_id(context, self.config.target_id)
else: if not page:
# Double-check under lock to avoid TOCTOU and ensure only # Fallback: create new page in existing context
# one task calls new_page when pages=[] concurrently
async with self._page_lock: async with self._page_lock:
pages = context.pages page = await context.new_page()
if pages: await self._apply_stealth_to_page(page)
page = pages[0] else:
elif self.config.browser_context_id and self.config.target_id: # Original logic for cases without pre-created target
# Pre-existing context/target provided - use CDP to get the page pages = context.pages
# This handles the case where Playwright doesn't see the target yet page = next((p for p in pages if p.url == crawlerRunConfig.url), None)
page = await self._get_page_by_target_id(context, self.config.target_id) if not page:
if not page: if pages:
# Fallback: create new page in existing context page = pages[0]
else:
# Double-check under lock to avoid TOCTOU and ensure only
# one task calls new_page when pages=[] concurrently
async with self._page_lock:
pages = context.pages
if pages:
page = pages[0]
else:
page = await context.new_page() page = await context.new_page()
await self._apply_stealth_to_page(page) await self._apply_stealth_to_page(page)
else:
page = await context.new_page()
await self._apply_stealth_to_page(page)
else: else:
# Otherwise, check if we have an existing context for this config # Otherwise, check if we have an existing context for this config
config_signature = self._make_config_signature(crawlerRunConfig) config_signature = self._make_config_signature(crawlerRunConfig)