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,6 +1184,18 @@ 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
# When target_id is provided, use it to find the specific page
# This is critical for concurrent requests sharing a browser
if self.config.browser_context_id and self.config.target_id:
page = await self._get_page_by_target_id(context, self.config.target_id)
if not page:
# Fallback: create new page in existing context
async with self._page_lock:
page = await context.new_page()
await self._apply_stealth_to_page(page)
else:
# Original logic for cases without pre-created target
pages = context.pages pages = context.pages
page = next((p for p in pages if p.url == crawlerRunConfig.url), None) page = next((p for p in pages if p.url == crawlerRunConfig.url), None)
if not page: if not page:
@@ -1196,14 +1208,6 @@ class BrowserManager:
pages = context.pages pages = context.pages
if pages: if pages:
page = pages[0] page = pages[0]
elif self.config.browser_context_id and self.config.target_id:
# Pre-existing context/target provided - use CDP to get the page
# This handles the case where Playwright doesn't see the target yet
page = await self._get_page_by_target_id(context, self.config.target_id)
if not page:
# Fallback: create new page in existing context
page = await context.new_page()
await self._apply_stealth_to_page(page)
else: 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)