From 1c0ce413286855bf3a7a3f796679f91243cb6e5b Mon Sep 17 00:00:00 2001 From: UncleCode Date: Tue, 20 May 2025 21:12:32 +0800 Subject: [PATCH] Fix managed browser page retrieval when no pages (#1137) This pull request addresses the issue of handling default context pages when none are open. - Introduces a conditional check to determine if a page exists in the context. - If no pages exist, a new page is created via await context.new_page(). --- crawl4ai/browser_manager.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crawl4ai/browser_manager.py b/crawl4ai/browser_manager.py index d4e074cf..3afa45c0 100644 --- a/crawl4ai/browser_manager.py +++ b/crawl4ai/browser_manager.py @@ -964,7 +964,10 @@ class BrowserManager: pages = context.pages page = next((p for p in pages if p.url == crawlerRunConfig.url), None) if not page: - page = context.pages[0] # await context.new_page() + if pages: + page = context.pages[0] + else: + page = await context.new_page() else: # Otherwise, check if we have an existing context for this config config_signature = self._make_config_signature(crawlerRunConfig)