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
Squashed commit of the following:
Magnus Müller4c2952d640bb
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex 38b907913..e64179203 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -1480,8 +1480,7 @@ class BrowserSession(BaseModel):"""page = await self.get_current_page()try:- await page.evaluate(- """+ script = """try {// Remove the highlight container and all its contentsconst container = document.getElementById('playwright-highlight-container');@@ -1498,7 +1497,13 @@ class BrowserSession(BaseModel):console.error('Failed to remove highlights:', e);}"""- )++ await page.evaluate(script)++ for iframe in page.frames:+ if iframe.url and iframe.url != page.url and not iframe.url.startswith('data:'):+ await iframe.evaluate(script)+except Exception as e:self.logger.debug(f'⚠️ Failed to remove highlights (this is usually ok): {type(e).__name__}: {e}')# Don't raise the error since this is not critical functionalitydiff --git a/browser_use/controller/service.py b/browser_use/controller/service.pyindex 92da630da..c9d9353b4 100644--- a/browser_use/controller/service.py+++ b/browser_use/controller/service.py@@ -155,9 +155,11 @@ class Controller(Generic[Context]):page = await browser_session.get_current_page()if page.url.strip('/') == 'https://www.google.com':- await page.goto(search_url)- await page.wait_for_load_state()+ # SECURITY FIX: Use browser_session.navigate_to() instead of direct page.goto()+ # This ensures URL validation against allowed_domains is performed+ await browser_session.navigate_to(search_url)else:+ # create_new_tab already includes proper URL validationpage = await browser_session.create_new_tab(search_url)msg = f'🔍 Searched for "{params.query}" in Google'@@ -169,15 +171,12 @@ class Controller(Generic[Context]):@self.registry.action('Navigate to URL in the current tab', param_model=GoToUrlAction)async def go_to_url(params: GoToUrlAction, browser_session: BrowserSession):try:- page = await browser_session.get_current_page()- if page:- await page.goto(params.url)- await page.wait_for_load_state()- else:- page = await browser_session.create_new_tab(params.url)+ # SECURITY FIX: Use browser_session.navigate_to() instead of direct page.goto()+ # This ensures URL validation against allowed_domains is performed+ await browser_session.navigate_to(params.url)msg = f'Navigated to {params.url}'logger.info(f'🔗 {msg}')- return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=msg)+ return ActionResult(extracted_content=msg)except Exception as e:error_msg = str(e)# Check for network-related errors@@ -197,7 +196,7 @@ class Controller(Generic[Context]):success=False, error=site_unavailable_msg, include_in_memory=True, long_term_memory=site_unavailable_msg)else:- # Re-raise non-network errors+ # Re-raise non-network errors (including URLNotAllowedError for unauthorized domains)raise@self.registry.action('Go back', param_model=NoParamsAction)diff --git a/browser_use/dom/buildDomTree.js b/browser_use/dom/buildDomTree.jsindex 57c147421..1dec1fe8d 100644--- a/browser_use/dom/buildDomTree.js+++ b/browser_use/dom/buildDomTree.js@@ -4,10 +4,11 @@focusHighlightIndex: -1,viewportExpansion: 0,debugMode: false,+ initialIndex: 0,}) => {- const { doHighlightElements, focusHighlightIndex, viewportExpansion, debugMode } = args;- let highlightIndex = 0; // Reset highlight index+ const { doHighlightElements, focusHighlightIndex, viewportExpansion, debugMode, initialIndex } = args;+ let highlightIndex = initialIndex; // Reset highlight index// Add timing stack to handle recursionconst TIMING_STACK = {@@ -210,7 +211,7 @@*/const DOM_HASH_MAP = {};- const ID = { current: 0 };+ const ID = { current: initialIndex };const HIGHLIGHT_CONTAINER_ID = "playwright-highlight-container";@@ -1354,6 +1355,7 @@if (domElement) nodeData.children.push(domElement);}}+ nodeData.hasIframeContent = true;} catch (e) {console.warn("Unable to access iframe:", e);}diff --git a/browser_use/dom/service.py b/browser_use/dom/service.pyindex b35c5a4eb..2da530e63 100644--- a/browser_use/dom/service.py+++ b/browser_use/dom/service.py@@ -6,6 +6,8 @@ from urllib.parse import urlparseif TYPE_CHECKING:from browser_use.browser.types import Page+from dataclasses import dataclass+from browser_use.dom.views import (DOMBaseNode,DOMElementNode,@@ -22,6 +24,42 @@ from browser_use.utils import time_execution_async# height: int+@dataclass+class PageFrameEvaluationResult:+ url: str+ result: dict+ name: str | None = None+ id: str | None = None++ @property+ def known_frame_urls(self) -> list[str]:+ return [+ v.get('attributes', {}).get('src')+ for v in self.map.values()+ if v.get('hasIframeContent') and v.get('attributes', {}).get('src')+ ]++ @property+ def map(self) -> dict:+ return self.result.get('map', {})++ @property+ def map_size(self) -> int:+ return len(self.map)++ @property+ def perf_metrics(self) -> dict:+ return self.result.get('perfMetrics', {})++ @property+ def short_url(self) -> str:+ return self.url[:50] + '...' if len(self.url) > 50 else self.url++ @property+ def root_id(self) -> str | None:+ return self.result.get('rootId')++class DomService:logger: logging.Logger@@ -94,73 +132,160 @@ class DomService:'focusHighlightIndex': focus_element,'viewportExpansion': viewport_expansion,'debugMode': debug_mode,+ 'initialIndex': 0,}try:eval_page: dict = await self.page.evaluate(self.js_code, args)+ page_eval_result = PageFrameEvaluationResult(+ url=self.page.url,+ result=eval_page,+ )except Exception as e:self.logger.error('Error evaluating JavaScript: %s', e)raise- # Only log performance metrics in debug mode- if debug_mode and 'perfMetrics' in eval_page:- perf = eval_page['perfMetrics']-- # Get key metrics for summary- total_nodes = perf.get('nodeMetrics', {}).get('totalNodes', 0)- # processed_nodes = perf.get('nodeMetrics', {}).get('processedNodes', 0)-- # Count interactive elements from the DOM map- interactive_count = 0- if 'map' in eval_page:- for node_data in eval_page['map'].values():- if isinstance(node_data, dict) and node_data.get('isInteractive'):- interactive_count += 1-- # Create concise summary- url_short = self.page.url[:50] + '...' if len(self.page.url) > 50 else self.page.url- self.logger.debug(- '🔎 Ran buildDOMTree.js interactive element detection on: %s interactive=%d/%d\n',- url_short,- interactive_count,- total_nodes,- # processed_nodes,- )+ frames = [page_eval_result]+ total_map_size = page_eval_result.map_size++ known_frame_urls = page_eval_result.known_frame_urls+ # TODO: only look in iframes from enabled_domains+ for iframe in self.page.frames:+ if (+ iframe.url+ and iframe.url != self.page.url+ and not iframe.url.startswith('data:')+ and iframe.url not in known_frame_urls+ ):+ try:+ frame_element = await iframe.frame_element()+ except Exception as e:+ self.logger.error('Error getting frame element for iframe %s: %s', iframe.url, e)+ continue++ if not await frame_element.is_visible():+ continue++ args['initialIndex'] = total_map_size # continue indexing from the last index+ try:+ name = await frame_element.get_attribute('name')+ id = await frame_element.get_attribute('id')+ iframe_eval_result = await iframe.evaluate(self.js_code, args)+ frame = PageFrameEvaluationResult(+ url=iframe.url,+ result=iframe_eval_result,+ name=name,+ id=id,+ )+ frames.append(frame)+ known_frame_urls.append(iframe.url)+ known_frame_urls.extend(frame.known_frame_urls)+ total_map_size += frame.map_size+ except Exception as e:+ self.logger.error('Error evaluating JavaScript in iframe %s: %s', iframe.url, e)+ continue- return await self._construct_dom_tree(eval_page)+ # Only log performance metrics in debug mode+ if debug_mode and len(frames) > 1:+ for index, frame in enumerate(frames):+ perf = frame.perf_metrics+ if perf:+ # Get key metrics for summary+ total_nodes = perf.get('nodeMetrics', {}).get('totalNodes', 0)+ # processed_nodes = perf.get('nodeMetrics', {}).get('processedNodes', 0)++ # Count interactive elements from the DOM map+ interactive_count = 0+ for node_data in frame.map.values():+ if isinstance(node_data, dict) and node_data.get('isInteractive'):+ interactive_count += 1++ # Create concise summary+ self.logger.debug(+ f'🔎 Ran buildDOMTree.js interactive element detection on{" iframe" if index > 0 else ""}: %s interactive=%d/%d\n',+ frame.short_url,+ interactive_count,+ total_nodes,+ # processed_nodes,+ )++ return await self._construct_dom_tree(frames)@time_execution_async('--construct_dom_tree')async def _construct_dom_tree(self,- eval_page: dict,+ frames: list[PageFrameEvaluationResult],) -> tuple[DOMElementNode, SelectorMap]:- js_node_map = eval_page['map']- js_root_id = eval_page['rootId']-- selector_map = {}- node_map = {}-- for id, node_data in js_node_map.items():- node, children_ids = self._parse_node(node_data)- if node is None:- continue-- node_map[id] = node-- if isinstance(node, DOMElementNode) and node.highlight_index is not None:- selector_map[node.highlight_index] = node-- # NOTE: We know that we are building the tree bottom up- # and all children are already processed.- if isinstance(node, DOMElementNode):- for child_id in children_ids:- if child_id not in node_map:+ # The first page in eval_pages is the main page, and it contains the rootId+ js_root_id = frames[0].root_id+ if js_root_id is None:+ raise ValueError('No rootId found in the evaluated page structure')++ selector_map: SelectorMap = {}+ node_map: dict[str, DOMBaseNode] = {}++ for frame in frames:+ js_node_map = frame.map+ for id, node_data in js_node_map.items():+ node, children_ids = self._parse_node(node_data)+ if node is None:+ continue++ node_map[id] = node++ if isinstance(node, DOMElementNode) and node.highlight_index is not None:+ selector_map[node.highlight_index] = node++ # NOTE: We know that we are building the tree bottom up+ # and all children are already processed.+ if isinstance(node, DOMElementNode):+ for child_id in children_ids:+ if child_id not in node_map:+ continue++ child_node = node_map[child_id]++ child_node.parent = node+ node.children.append(child_node)++ # For each child iframe, we need to set the parent of the root element to the iframe element.+ for frame in frames[1:]:+ content_root_node = node_map.get(frame.root_id)+ if content_root_node:+ # Find the iframe element in the main page+ iframe_element_node = next(+ (+ node+ for node in node_map.values()+ if isinstance(node, DOMElementNode)+ and node.is_iframe_element(url=frame.url, name=frame.name, id=frame.id)+ ),+ None,+ )+ if iframe_element_node:+ if not iframe_element_node.children:+ iframe_element_node.children = [content_root_node]+ content_root_node.parent = iframe_element_nodecontinue-- child_node = node_map[child_id]-- child_node.parent = node- node.children.append(child_node)+ else:+ self.logger.warning(+ 'Iframe element %s already has children, skipping',+ frame.short_url,+ )+ else:+ self.logger.warning(+ 'Could not find iframe element for %s in the main page DOM',+ frame.short_url,+ )++ # If we could not find the iframe element, remove the frame's nodes from the maps.+ for id in frame.map.keys():+ node = node_map.get(id)+ # Remove the node from the selector map if it has a highlight index+ if isinstance(node, DOMElementNode) and node.highlight_index is not None and node.highlight_index in selector_map:+ del selector_map[node.highlight_index]++ del node_map[id]html_to_dict = node_map[str(js_root_id)]diff --git a/browser_use/dom/views.py b/browser_use/dom/views.pyindex 3d2cfece4..b9948054c 100644--- a/browser_use/dom/views.py+++ b/browser_use/dom/views.py@@ -230,6 +230,14 @@ class DOMElementNode(DOMBaseNode):process_node(self, 0)return '\n'.join(formatted_text)+ def is_iframe_element(self, url: str, name: str | None = None, id: str | None = None) -> bool:+ return (+ self.tag_name.lower() == 'iframe'+ and self.attributes.get('src') == url+ and (name is None or self.attributes.get('name') == name)+ and (id is None or self.attributes.get('id') == id)+ )+SelectorMap = dict[int, DOMElementNode]diff --git a/eval/service.py b/eval/service.pyindex 2e51e807f..1504b307a 100644--- a/eval/service.py+++ b/eval/service.py@@ -291,7 +291,7 @@ from langchain_google_genai import ChatGoogleGenerativeAIfrom langchain_openai import ChatOpenAIfrom pydantic.types import SecretStr-from browser_use import ActionResult, Agent, BrowserProfile, BrowserSession, Controller+from browser_use import ActionResult, Agent, BrowserSession, Controllerfrom browser_use.agent.memory import MemoryConfigfrom browser_use.agent.views import AgentHistoryList@@ -1075,15 +1075,11 @@ async def load_existing_result(task_folder: Path) -> dict:async def setup_browser_session(task: Task, headless: bool) -> BrowserSession:"""Setup browser session for the task"""- logger.debug(f'Browser setup: Creating unique user data directory for task {task.task_id}')- # Create unique user data directory- base_user_data_dir = Path(BrowserProfile().user_data_dir).parent- unique_user_data_dir = base_user_data_dir / f'task_{task.task_id}'- unique_user_data_dir.mkdir(parents=True, exist_ok=True)-logger.debug(f'Browser setup: Initializing BrowserSession for task {task.task_id}')++ # Use incognito mode (user_data_dir=None) for evaluations to avoid state pollutionbrowser_session = BrowserSession(- user_data_dir=str(unique_user_data_dir),+ user_data_dir=None, # Incognito mode - no persistent stateheadless=headless,chromium_sandbox=False, # running in docker# higher timeouts = higher success rates on long tail of slow sites or if on a slow CI server