Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fixed dom service cache
Gregor Žunič7aeb0eb9e8f7
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 40a14e9ab..1202dc8dc 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -3297,6 +3297,8 @@ class BrowserSession(BaseModel):async def _get_updated_state(self, focus_element: int = -1, include_screenshot: bool = True) -> BrowserStateSummary:"""Update and return state."""+ dom_service: DomService | None = None+# Check if current page is still valid, if not switch to another available pagepage = await self.get_current_page()@@ -3472,7 +3474,16 @@ class BrowserSession(BaseModel):return self.browser_state_summaryraisefinally:- await self.remove_highlights(dom_service)+ if dom_service:+ await self.remove_highlights(dom_service)++ else:+ try:+ async with DomService(self, page) as dom_service:+ await self.remove_highlights(dom_service)+ except Exception:+ self.logger.debug("Can't remove highlights, probably the dom_service is broken")+ pass# region - Page Health Check Helpers@observe_debug(ignore_input=True)diff --git a/browser_use/dom/service.py b/browser_use/dom/service.pyindex f6c5c111e..b43a7a7f2 100644--- a/browser_use/dom/service.py+++ b/browser_use/dom/service.py@@ -53,7 +53,8 @@ class DomService:self.cdp_client: CDPClient | None = None# self.playwright_page_to_session_id_store: dict[str, str] = {}- self.target_to_session_id_cache: dict[str, str] = {}+ # self.target_to_session_id_cache: dict[str, str] = {}+ self.session_id_domains_enabled_cache: dict[str, bool] = {}async def _get_cdp_client(self) -> CDPClient:if not self.browser.cdp_url:@@ -123,12 +124,22 @@ class DomService:target_id = str(target['targetId'])- if target_id in self.target_to_session_id_cache:- return self.target_to_session_id_cache[target_id]+ # if target_id in self.target_to_session_id_cache:+ # return self.target_to_session_id_cache[target_id]session = await cdp_client.send.Target.attachToTarget(params={'targetId': target_id, 'flatten': True})session_id = session['sessionId']+ await self._enable_all_domains_on_session(session_id)++ return session_id++ async def _enable_all_domains_on_session(self, session_id: str) -> None:+ if session_id in self.session_id_domains_enabled_cache:+ return++ cdp_client = await self._get_cdp_client()+await cdp_client.send.Target.setAutoAttach(params={'autoAttach': True,@@ -145,9 +156,7 @@ class DomService:cdp_client.send.Page.enable(session_id=session_id),)- self.target_to_session_id_cache[target_id] = session_id-- return session_id+ self.session_id_domains_enabled_cache[session_id] = Truedef _build_enhanced_ax_node(self, ax_node: AXNode) -> EnhancedAXNode:properties: list[EnhancedAXProperty] | None = None