{"record":{"id":"f08fb41e7cf25ac7","repo":"browser-use/browser-use","slug":"no-current-target-found","errorCode":null,"errorMessage":"No current target found","messagePattern":"No current target found","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"browser_use/browser/session.py","lineNumber":1365,"sourceCode":"\n\t\treturn Target(self, target_id)\n\n\tasync def get_current_page(self) -> 'Page | None':\n\t\t\"\"\"Get the current page as an actor Page.\"\"\"\n\t\ttarget_info = await self.get_current_target_info()\n\n\t\tif not target_info:\n\t\t\treturn None\n\n\t\tfrom browser_use.actor.page import Page as Target\n\n\t\treturn Target(self, target_info['targetId'])\n\n\tasync def must_get_current_page(self) -> 'Page':\n\t\t\"\"\"Get the current page as an actor Page.\"\"\"\n\t\tpage = await self.get_current_page()\n\t\tif not page:\n\t\t\traise RuntimeError('No current target found')\n\n\t\treturn page\n\n\tasync def get_pages(self) -> list['Page']:\n\t\t\"\"\"Get all available pages using SessionManager (source of truth).\"\"\"\n\t\t# Import here to avoid circular import\n\t\tfrom browser_use.actor.page import Page as PageActor\n\n\t\tpage_targets = self.session_manager.get_all_page_targets() if self.session_manager else []\n\n\t\ttargets = []\n\t\tfor target in page_targets:\n\t\t\ttargets.append(PageActor(self, target.target_id))\n\n\t\treturn targets\n\n\tdef get_focused_target(self) -> 'Target | None':\n\t\t\"\"\"Get the target that currently has agent focus.","sourceCodeStart":1347,"sourceCodeEnd":1383,"githubUrl":"https://github.com/browser-use/browser-use/blob/6c73fced2f6d45a11d88622fe56365a5fe18f28b/browser_use/browser/session.py#L1347-L1383","documentation":"must_get_current_page() is the strict variant of get_current_page(): it resolves the CDP target matching the current agent focus and raises RuntimeError('No current target found') when that resolution yields nothing. It fires when there is no focus target or the focus target no longer exists among known targets.","triggerScenarios":"Calling must_get_current_page() (or APIs built on it) before start() finishes, after the focused tab was closed/crashed, or when the SessionManager has no page targets matching the current focus.","commonSituations":"Agent code taking screenshots or extracting DOM right after the last tab was closed by the page itself (window.close, target=_blank flows); session used after browser crash; race during startup.","solutions":["Use get_current_page() and handle None instead of must_get_current_page() when absence is expected","Ensure at least one tab exists before acting: open about:blank if page_targets is empty","After a tab closes, re-establish focus (switch_tab to most recent) before further page operations","Verify browser.agent_focus_target_id is set and the browser is connected"],"exampleFix":"// before\npage = await browser.must_get_current_page()  # raises when focus is gone\n\n// after\npage = await browser.get_current_page()\nif page is None:\n    # focus lost — recover by opening/switching to a tab\n    await browser.switch_tab(None)  # most recent, or create new\n    page = await browser.must_get_current_page()","handlingStrategy":"type-guard","validationCode":"if await browser.get_current_page() is None:\n    await browser.switch_tab(None)  # recover focus to most recent/new tab","typeGuard":"async def has_current_page(browser) -> bool:\n    return await browser.get_current_page() is not None","tryCatchPattern":"try:\n    page = await browser.must_get_current_page()\nexcept RuntimeError as e:\n    if 'No current target found' in str(e):\n        page = None  # or recover focus then retry\n    else:\n        raise","preventionTips":["Prefer get_current_page() with None handling","Keep at least one tab open","Re-establish focus after tab-closing flows"],"tags":["lifecycle","tabs","focus","cdp"],"backgroundTag":null,"analyzedSha":"6c73fced2f6d45a11d88622fe56365a5fe18f28b","analyzedAt":"2026-08-14T19:42:40.557Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}