Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

2 upload function

Magnus Müller4d5261418eff
diff --git a/browser_use/tools/service.py b/browser_use/tools/service.py
index 9476dfbd1..e39f4ce6b 100644
--- a/browser_use/tools/service.py
+++ b/browser_use/tools/service.py
@@ -357,47 +357,38 @@ class Tools(Generic[Context]):
param_model=UploadFileAction,
)
async def upload_file(
- params: UploadFileAction,
- browser_session: BrowserSession,
- available_file_paths: list[str],
- file_system: FileSystem,
+ params: UploadFileAction, browser_session: BrowserSession, available_file_paths: list[str], file_system: FileSystem
):
- # Path validation logic:
- # 1. If available_file_paths provided (security mode), enforce it as a whitelist
- # 2. If no whitelist, for local browsers just check file exists
- # 3. For remote browsers, allow any path (assume it exists remotely)
-
- # If whitelist provided, validate path is in it
- if available_file_paths:
- if params.path not in available_file_paths:
- # Also check if it's a recently downloaded file
- downloaded_files = browser_session.downloaded_files
- if params.path not in downloaded_files:
- # Finally, check if it's a file in the FileSystem service (if provided)
- if file_system is not None and file_system.get_dir():
- # Check if the file is actually managed by the FileSystem service
- # The path should be just the filename for FileSystem files
- file_obj = file_system.get_file(params.path)
- if file_obj:
- # File is managed by FileSystem, construct the full path
- file_system_path = str(file_system.get_dir() / params.path)
- params = UploadFileAction(index=params.index, path=file_system_path)
- else:
- # If browser is remote, allow passing a remote-accessible absolute path
- if not browser_session.is_local:
- pass
- else:
- msg = f'File path {params.path} is not available. Upload files must be in available_file_paths, downloaded_files, or a file managed by file_system.'
- logger.error(f'❌ {msg}')
- return ActionResult(error=msg)
+ # Check if file is in available_file_paths (user-provided or downloaded files)
+ # For remote browsers (is_local=False), we allow absolute remote paths even if not tracked locally
+ if params.path not in available_file_paths:
+ # Also check if it's a recently downloaded file that might not be in available_file_paths yet
+ downloaded_files = browser_session.downloaded_files
+ if params.path not in downloaded_files:
+ # Finally, check if it's a file in the FileSystem service
+ if file_system and file_system.get_dir():
+ # Check if the file is actually managed by the FileSystem service
+ # The path should be just the filename for FileSystem files
+ file_obj = file_system.get_file(params.path)
+ if file_obj:
+ # File is managed by FileSystem, construct the full path
+ file_system_path = str(file_system.get_dir() / params.path)
+ params = UploadFileAction(index=params.index, path=file_system_path)
else:
# If browser is remote, allow passing a remote-accessible absolute path
if not browser_session.is_local:
pass
else:
- msg = f'File path {params.path} is not available. Upload files must be in available_file_paths or downloaded_files.'
+ msg = f'File path {params.path} is not available. Upload files must be in available_file_paths, downloaded_files, or a file managed by file_system.'
logger.error(f'❌ {msg}')
return ActionResult(error=msg)
+ else:
+ # If browser is remote, allow passing a remote-accessible absolute path
+ if not browser_session.is_local:
+ pass
+ else:
+ msg = f'File path {params.path} is not available. Upload files must be in available_file_paths, downloaded_files, or a file managed by file_system.'
+ raise BrowserError(message=msg, long_term_memory=msg)
# For local browsers, ensure the file exists on the local filesystem
if browser_session.is_local:
@@ -1414,3 +1405,169 @@ class CodeAgentTools(Tools[Context]):
long_term_memory=memory,
attachments=attachments,
)
+
+ # Override upload_file for code agent with relaxed path validation
+ @self.registry.action(
+ 'Upload a file to a file input element. For code-use mode, any file accessible from the current directory can be uploaded.',
+ param_model=UploadFileAction,
+ )
+ async def upload_file(
+ params: UploadFileAction,
+ browser_session: BrowserSession,
+ available_file_paths: list[str],
+ file_system: FileSystem,
+ ):
+ # Path validation logic for code-use mode:
+ # 1. If available_file_paths provided (security mode), enforce it as a whitelist
+ # 2. If no whitelist, for local browsers just check file exists
+ # 3. For remote browsers, allow any path (assume it exists remotely)
+
+ # If whitelist provided, validate path is in it
+ if available_file_paths:
+ if params.path not in available_file_paths:
+ # Also check if it's a recently downloaded file
+ downloaded_files = browser_session.downloaded_files
+ if params.path not in downloaded_files:
+ # Finally, check if it's a file in the FileSystem service (if provided)
+ if file_system is not None and file_system.get_dir():
+ # Check if the file is actually managed by the FileSystem service
+ # The path should be just the filename for FileSystem files
+ file_obj = file_system.get_file(params.path)
+ if file_obj:
+ # File is managed by FileSystem, construct the full path
+ file_system_path = str(file_system.get_dir() / params.path)
+ params = UploadFileAction(index=params.index, path=file_system_path)
+ else:
+ # If browser is remote, allow passing a remote-accessible absolute path
+ if not browser_session.is_local:
+ pass
+ else:
+ msg = f'File path {params.path} is not available. Upload files must be in available_file_paths, downloaded_files, or a file managed by file_system.'
+ logger.error(f'❌ {msg}')
+ return ActionResult(error=msg)
+ else:
+ # If browser is remote, allow passing a remote-accessible absolute path
+ if not browser_session.is_local:
+ pass
+ else:
+ msg = f'File path {params.path} is not available. Upload files must be in available_file_paths or downloaded_files.'
+ logger.error(f'❌ {msg}')
+ return ActionResult(error=msg)
+
+ # For local browsers, ensure the file exists on the local filesystem
+ if browser_session.is_local:
+ if not os.path.exists(params.path):
+ msg = f'File {params.path} does not exist'
+ return ActionResult(error=msg)
+
+ # Get the selector map to find the node
+ selector_map = await browser_session.get_selector_map()
+ if params.index not in selector_map:
+ msg = f'Element with index {params.index} does not exist.'
+ return ActionResult(error=msg)
+
+ node = selector_map[params.index]
+
+ # Helper function to find file input near the selected element
+ def find_file_input_near_element(
+ node: EnhancedDOMTreeNode, max_height: int = 3, max_descendant_depth: int = 3
+ ) -> EnhancedDOMTreeNode | None:
+ """Find the closest file input to the selected element."""
+
+ def find_file_input_in_descendants(n: EnhancedDOMTreeNode, depth: int) -> EnhancedDOMTreeNode | None:
+ if depth < 0:
+ return None
+ if browser_session.is_file_input(n):
+ return n
+ for child in n.children_nodes or []:
+ result = find_file_input_in_descendants(child, depth - 1)
+ if result:
+ return result
+ return None
+
+ current = node
+ for _ in range(max_height + 1):
+ # Check the current node itself
+ if browser_session.is_file_input(current):
+ return current
+ # Check all descendants of the current node
+ result = find_file_input_in_descendants(current, max_descendant_depth)
+ if result:
+ return result
+ # Check all siblings and their descendants
+ if current.parent_node:
+ for sibling in current.parent_node.children_nodes or []:
+ if sibling is current:
+ continue
+ if browser_session.is_file_input(sibling):
+ return sibling
+ result = find_file_input_in_descendants(sibling, max_descendant_depth)
+ if result:
+ return result
+ current = current.parent_node
+ if not current:
+ break
+ return None
+
+ # Try to find a file input element near the selected element
+ file_input_node = find_file_input_near_element(node)
+
+ # Highlight the file input element if found (truly non-blocking)
+ if file_input_node:
+ asyncio.create_task(browser_session.highlight_interaction_element(file_input_node))
+
+ # If not found near the selected element, fallback to finding the closest file input to current scroll position
+ if file_input_node is None:
+ logger.info(
+ f'No file upload element found near index {params.index}, searching for closest file input to scroll position'
+ )
+
+ # Get current scroll position
+ cdp_session = await browser_session.get_or_create_cdp_session()
+ try:
+ scroll_info = await cdp_session.cdp_client.send.Runtime.evaluate(
+ params={'expression': 'window.scrollY || window.pageYOffset || 0'}, session_id=cdp_session.session_id
+ )
+ current_scroll_y = scroll_info.get('result', {}).get('value', 0)
+ except Exception:
+ current_scroll_y = 0
+
+ # Find all file inputs in the selector map and pick the closest one to scroll position
+ closest_file_input = None
+ min_distance = float('inf')
+
+ for idx, element in selector_map.items():
+ if browser_session.is_file_input(element):
+ # Get element's Y position
+ if element.absolute_position:
+ element_y = element.absolute_position.y
+ distance = abs(element_y - current_scroll_y)
+ if distance < min_distance:
+ min_distance = distance
+ closest_file_input = element
+
+ if closest_file_input:
+ file_input_node = closest_file_input
+ logger.info(f'Found file input closest to scroll position (distance: {min_distance}px)')
+ # Highlight the fallback file input element (truly non-blocking)
+ asyncio.create_task(browser_session.highlight_interaction_element(file_input_node))
+ else:
+ msg = 'No file upload element found on the page'
+ logger.error(msg)
+ raise BrowserError(msg)
+ # TODO: figure out why this fails sometimes + add fallback hail mary, just look for any file input on page
+
+ # Dispatch upload file event with the file input node
+ try:
+ event = browser_session.event_bus.dispatch(UploadFileEvent(node=file_input_node, file_path=params.path))
+ await event
+ await event.event_result(raise_if_any=True, raise_if_none=False)
+ msg = f'Successfully uploaded file to index {params.index}'
+ logger.info(f'📁 {msg}')
+ return ActionResult(
+ extracted_content=msg,
+ long_term_memory=f'Uploaded file {params.path} to element {params.index}',
+ )
+ except Exception as e:
+ logger.error(f'Failed to upload file: {e}')
+ raise BrowserError(f'Failed to upload file: {e}')