Fix: use CDP to find context by browserContextId for concurrent sessions
This commit is contained in:
@@ -677,34 +677,76 @@ class BrowserManager:
|
|||||||
tag="BROWSER"
|
tag="BROWSER"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Find the specific context by matching target_id to pages
|
# Find the specific context by matching browserContextId
|
||||||
# This is critical for concurrent crawls sharing a browser
|
# Each Playwright context has pages, and we need to find which one
|
||||||
|
# corresponds to our CDP browserContextId
|
||||||
found_context = None
|
found_context = None
|
||||||
if self.config.target_id and contexts:
|
if contexts and len(contexts) == 1:
|
||||||
for ctx in contexts:
|
# Only one context - use it directly
|
||||||
for page in ctx.pages:
|
found_context = contexts[0]
|
||||||
# Playwright stores target ID in internal implementation
|
elif contexts and len(contexts) > 1:
|
||||||
page_impl = getattr(page, '_impl_obj', None)
|
# Multiple contexts - need to find the right one
|
||||||
page_target_id = getattr(page_impl, '_target_id', None) if page_impl else None
|
# Use CDP to query which context owns our target
|
||||||
if page_target_id == self.config.target_id:
|
try:
|
||||||
found_context = ctx
|
# Get first page from any context to create CDP session
|
||||||
if self.logger:
|
any_page = None
|
||||||
self.logger.debug(
|
for ctx in contexts:
|
||||||
f"Found context by target_id: {self.config.target_id}",
|
if ctx.pages:
|
||||||
tag="BROWSER"
|
any_page = ctx.pages[0]
|
||||||
)
|
|
||||||
break
|
break
|
||||||
if found_context:
|
|
||||||
break
|
if any_page:
|
||||||
|
cdp = await any_page.context.new_cdp_session(any_page)
|
||||||
|
try:
|
||||||
|
result = await cdp.send("Target.getTargets")
|
||||||
|
targets = result.get("targetInfos", [])
|
||||||
|
|
||||||
|
# Find our target and its browserContextId
|
||||||
|
for target in targets:
|
||||||
|
if target.get("targetId") == self.config.target_id:
|
||||||
|
target_browser_context_id = target.get("browserContextId")
|
||||||
|
if target_browser_context_id == self.config.browser_context_id:
|
||||||
|
# Found it - now find which Playwright context has a page matching
|
||||||
|
for ctx in contexts:
|
||||||
|
for page in ctx.pages:
|
||||||
|
# Check if this page's context matches
|
||||||
|
# by checking if the page is in the right context
|
||||||
|
page_cdp = await ctx.new_cdp_session(page)
|
||||||
|
try:
|
||||||
|
page_targets = await page_cdp.send("Target.getTargets")
|
||||||
|
for pt in page_targets.get("targetInfos", []):
|
||||||
|
if pt.get("browserContextId") == self.config.browser_context_id:
|
||||||
|
found_context = ctx
|
||||||
|
break
|
||||||
|
finally:
|
||||||
|
await page_cdp.detach()
|
||||||
|
if found_context:
|
||||||
|
break
|
||||||
|
if found_context:
|
||||||
|
break
|
||||||
|
break
|
||||||
|
finally:
|
||||||
|
await cdp.detach()
|
||||||
|
except Exception as e:
|
||||||
|
if self.logger:
|
||||||
|
self.logger.warning(
|
||||||
|
f"Failed to find context via CDP: {e}",
|
||||||
|
tag="BROWSER"
|
||||||
|
)
|
||||||
|
|
||||||
if found_context:
|
if found_context:
|
||||||
self.default_context = found_context
|
self.default_context = found_context
|
||||||
|
if self.logger:
|
||||||
|
self.logger.debug(
|
||||||
|
f"Found context for browserContextId: {self.config.browser_context_id}",
|
||||||
|
tag="BROWSER"
|
||||||
|
)
|
||||||
elif contexts:
|
elif contexts:
|
||||||
# Fallback to first context if we can't find the specific one
|
# Fallback to first context if we can't find the specific one
|
||||||
self.default_context = contexts[0]
|
self.default_context = contexts[0]
|
||||||
if self.logger:
|
if self.logger:
|
||||||
self.logger.warning(
|
self.logger.warning(
|
||||||
f"Could not find context for target_id {self.config.target_id}, "
|
f"Could not find context for browserContextId {self.config.browser_context_id}, "
|
||||||
f"using first of {len(contexts)} context(s)",
|
f"using first of {len(contexts)} context(s)",
|
||||||
tag="BROWSER"
|
tag="BROWSER"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user