Skip to content

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

fix click element upload_index problem update file upload example

mertunsall15d25f4b613a
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index 10e9da65b..4c39e410a 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -3079,6 +3079,23 @@ class BrowserSession(BaseModel):
element_handle = await self.get_locate_element(selector_map[index])
return element_handle
+ async def is_file_input_by_index(self, index: int) -> bool:
+ try:
+ selector_map = await self.get_selector_map()
+ node = selector_map[index]
+ return self.is_file_input(node)
+ except Exception as e:
+ self.logger.debug(f'❌ Error in is_file_input(index={index}): {type(e).__name__}: {e}')
+ return False
+
+ @staticmethod
+ def is_file_input(node: DOMElementNode) -> bool:
+ return (
+ isinstance(node, DOMElementNode)
+ and getattr(node, 'tag_name', '').lower() == 'input'
+ and node.attributes.get('type', '').lower() == 'file'
+ )
+
@require_initialization
async def find_file_upload_element_by_index(
self, index: int, max_height: int = 3, max_descendant_depth: int = 3
@@ -3098,17 +3115,10 @@ class BrowserSession(BaseModel):
candidate_element = selector_map[index]
- def is_file_input(node: DOMElementNode) -> bool:
- return (
- isinstance(node, DOMElementNode)
- and getattr(node, 'tag_name', '').lower() == 'input'
- and node.attributes.get('type', '').lower() == 'file'
- )
-
def find_file_input_in_descendants(node: DOMElementNode, depth: int) -> DOMElementNode | None:
if depth < 0 or not isinstance(node, DOMElementNode):
return None
- if is_file_input(node):
+ if self.is_file_input(node):
return node
for child in getattr(node, 'children', []):
result = find_file_input_in_descendants(child, depth - 1)
@@ -3119,7 +3129,7 @@ class BrowserSession(BaseModel):
current = candidate_element
for _ in range(max_height + 1): # include the candidate itself
# 1. Check the current node itself
- if is_file_input(current):
+ if self.is_file_input(current):
return current
# 2. Check all descendants of the current node
result = find_file_input_in_descendants(current, max_descendant_depth)
@@ -3131,7 +3141,7 @@ class BrowserSession(BaseModel):
for sibling in getattr(parent, 'children', []):
if sibling is current:
continue
- if is_file_input(sibling):
+ if self.is_file_input(sibling):
return sibling
result = find_file_input_in_descendants(sibling, max_descendant_depth)
if result:
diff --git a/browser_use/controller/service.py b/browser_use/controller/service.py
index ce2cdc47f..b1e13b35d 100644
--- a/browser_use/controller/service.py
+++ b/browser_use/controller/service.py
@@ -240,7 +240,7 @@ class Controller(Generic[Context]):
initial_pages = len(browser_session.tabs)
# if element has file uploader then dont click
- if await browser_session.find_file_upload_element_by_index(params.index) is not None:
+ if await browser_session.is_file_input_by_index(params.index):
msg = f'Index {params.index} - has an element which opens file upload dialog. To upload files please use a specific function to upload files '
logger.info(msg)
return ActionResult(extracted_content=msg, include_in_memory=True, success=False, long_term_memory=msg)
diff --git a/examples/custom-functions/file_upload.py b/examples/custom-functions/file_upload.py
index 6f0d628ff..aee4b6b60 100644
--- a/examples/custom-functions/file_upload.py
+++ b/examples/custom-functions/file_upload.py
@@ -10,7 +10,13 @@ from dotenv import load_dotenv
load_dotenv()
-import anyio
+from lmnr import Laminar
+
+try:
+ Laminar.initialize(project_api_key=os.getenv('LMNR_PROJECT_API_KEY'))
+except Exception:
+ pass
+
from langchain_openai import ChatOpenAI
from browser_use import Agent, Controller
@@ -22,9 +28,7 @@ logger = logging.getLogger(__name__)
controller = Controller()
- 'Upload file to interactive element with file path ',
-)
[email protected]('Upload file to interactive element with file path')
async def upload_file(index: int, path: str, browser_session: BrowserSession, available_file_paths: list[str]):
if path not in available_file_paths:
return ActionResult(error=f'File path {path} is not available')
@@ -57,18 +61,6 @@ async def upload_file(index: int, path: str, browser_session: BrowserSession, av
return ActionResult(error=msg)
[email protected]('Read the file content of a file given a path')
-async def read_file(path: str, available_file_paths: list[str]):
- if path not in available_file_paths:
- return ActionResult(error=f'File path {path} is not available')
-
- async with await anyio.open_file(path, 'r') as f:
- content = await f.read()
- msg = f'File content: {content}'
- logger.info(msg)
- return ActionResult(extracted_content=msg, include_in_memory=True)
-
-
def create_file(file_type: str = 'txt'):
with open(f'tmp.{file_type}', 'w') as f:
f.write('test')
@@ -79,11 +71,10 @@ def create_file(file_type: str = 'txt'):
async def main():
task = 'Go to https://kzmpmkh2zfk1ojnpxfn1.lite.vusercontent.net/ and - read the file content and upload them to fields'
- task = 'Go to https://www.freepdfconvert.com/, upload the file tmp.pdf into the field choose a file - dont click the fileupload button'
-
+ task = 'Go to https://www.freepdfconvert.com/, upload the file tmp.pdf into the field choose a file - dont click the fileupload button'
available_file_paths = [create_file('txt'), create_file('pdf'), create_file('csv')]
- model = ChatOpenAI(model='gpt-4o')
+ model = ChatOpenAI(model='gpt-4.1-mini')
agent = Agent(
task=task,
llm=model,