Skip to content

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

Merge branch 'main' into mert/remove_save_pdf

mertunsalldd0f65b95627
diff --cc browser_use/controller/service.py
index aa68a2ad8,d816f49fa..0f81c7ef3
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@@ -2,6 -2,8 +2,7 @@@ import asynci
import enum
import json
import logging
+ import os
-import re
from collections.abc import Awaitable, Callable
from typing import Any, Generic, TypeVar, cast
@@@ -244,6 -247,59 +246,44 @@@ class Controller(Generic[Context])
long_term_memory=f"Input '{params.text}' into element {params.index}.",
)
+ @self.registry.action('Upload file to interactive element with file path', param_model=UploadFileAction)
+ async def upload_file(params: UploadFileAction, browser_session: BrowserSession, available_file_paths: list[str]):
+ if params.path not in available_file_paths:
+ return ActionResult(error=f'File path {params.path} is not available')
+
+ if not os.path.exists(params.path):
+ return ActionResult(error=f'File {params.path} does not exist')
+
+ file_upload_dom_el = await browser_session.find_file_upload_element_by_index(
+ params.index, max_height=3, max_descendant_depth=3
+ )
+
+ if file_upload_dom_el is None:
+ msg = f'No file upload element found at index {params.index}'
+ logger.info(msg)
+ return ActionResult(error=msg)
+
+ file_upload_el = await browser_session.get_locate_element(file_upload_dom_el)
+
+ if file_upload_el is None:
+ msg = f'No file upload element found at index {params.index}'
+ logger.info(msg)
+ return ActionResult(error=msg)
+
+ try:
+ await file_upload_el.set_input_files(params.path)
+ msg = f'📁 Successfully uploaded file to index {params.index}'
+ logger.info(msg)
+ return ActionResult(
+ extracted_content=msg,
+ include_in_memory=True,
+ long_term_memory=f'Uploaded file {params.path} to element {params.index}',
+ )
+ except Exception as e:
+ msg = f'Failed to upload file to index {params.index}: {str(e)}'
+ logger.info(msg)
+ return ActionResult(error=msg)
+
- # Save PDF
- @self.registry.action('Save the current page as a PDF file')
- async def save_pdf(page: Page):
- short_url = re.sub(r'^https?://(?:www\.)?|/$', '', page.url)
- slug = re.sub(r'[^a-zA-Z0-9]+', '-', short_url).strip('-').lower()
- sanitized_filename = f'{slug}.pdf'
-
- await page.emulate_media(media='screen')
- await page.pdf(path=sanitized_filename, format='A4', print_background=False)
- msg = f'Saving page with URL {page.url} as PDF to ./{sanitized_filename}'
- logger.info(msg)
- return ActionResult(
- extracted_content=msg, include_in_memory=True, long_term_memory=f'Saved PDF to {sanitized_filename}'
- )
-
# Tab Management Actions
@self.registry.action('Switch tab', param_model=SwitchTabAction)
async def switch_tab(params: SwitchTabAction, browser_session: BrowserSession):