Stealth browser automation for AI agents over MCP and the Chrome DevTools Protocol: navigation, network hooks, DOM extraction, and pixel-accurate UI cloning.
Python ai-agent-toolsai-toolsanthropicanti-bot-bypassautomation-toolsbrowser-automationchrome-devtoolsclaude-mcpcloudflare-bypassdeveloper-toolsfastmcpmcp-servermodel-context-protocolnetwork-interceptionnodriverpythonstealth-browserundetectable-browserweb-automationweb-scraping
feat(element-interaction): add file_upload tool
Ansonb0ebef399ae3
diff --git a/src/dom_handler.py b/src/dom_handler.pyindex 3c0092c..f09b1ae 100644--- a/src/dom_handler.py+++ b/src/dom_handler.py@@ -2,6 +2,7 @@import asyncioimport json+import osimport timefrom typing import List, Optional, Dict, Any@@ -356,6 +357,72 @@ class DOMHandler:except Exception as e:raise Exception(f"Failed to paste text: {str(e)}")+ @staticmethod+ async def file_upload(+ tab: Tab,+ selector: str,+ paths: List[str],+ ) -> Dict[str, Any]:+ """+ Upload local files to a <input type="file"> element via CDP DOM.setFileInputFiles.++ Resolves the actual file input even when the selector matches a wrapper+ (Workday, Ashby, LinkedIn often hide the real input behind a styled button).+ Refuses multi-file upload when the input lacks the `multiple` attribute+ (avoids browser crash documented in nodriver send_file()).++ Args:+ tab (Tab): The browser tab object.+ selector (str): CSS selector for the file input or any ancestor that contains it.+ paths (List[str]): Absolute local file paths to upload (single or multiple).++ Returns:+ Dict[str, Any]: { success: True, count: N, files: [basename, ...] }+ """+ try:+ if not paths:+ raise Exception("paths must contain at least one file")+ for p in paths:+ if not os.path.isabs(p):+ raise Exception(f"Path must be absolute: {p}")+ if not os.path.exists(p):+ raise Exception(f"File does not exist: {p}")++ element = await tab.select(selector)+ if not element:+ raise Exception(f"Element not found: {selector}")++ tag_name = element.tag_name+ input_type = (element.attrs.get('type') or '').lower()+ if tag_name != 'input' or input_type != 'file':+ inner_input = await element.query_selector('input[type="file"]')+ if inner_input:+ element = inner_input+ else:+ raise Exception(+ f"Selector did not resolve to a file input "+ f"(got <{tag_name} type=\"{input_type}\">). "+ f"Pass a selector that matches the <input type=\"file\"> "+ f"or one of its ancestors."+ )++ if len(paths) > 1 and 'multiple' not in element.attrs:+ raise Exception(+ f"File input does not accept multiple files "+ f"(no 'multiple' attribute), but {len(paths)} paths were "+ f"provided. Pass one path or upload sequentially."+ )++ await element.send_file(*paths)+ return {+ "success": True,+ "count": len(paths),+ "files": [os.path.basename(p) for p in paths],+ }++ except Exception as e:+ raise Exception(f"Failed to upload file(s): {str(e)}") from e+@staticmethodasync def select_option(tab: Tab,diff --git a/src/server.py b/src/server.pyindex 722f248..ec71071 100644--- a/src/server.py+++ b/src/server.py@@ -497,6 +497,32 @@ async def paste_text(raise Exception(f"Instance not found: {instance_id}")return await dom_handler.paste_text(tab, selector, text, clear_first)+@section_tool("element-interaction")+async def file_upload(+ instance_id: str,+ selector: str,+ paths: List[str]+) -> Dict[str, Any]:+ """+ Upload local files to a <input type="file"> element via CDP DOM.setFileInputFiles.++ Resolves the actual file input even if the selector matches a wrapper element+ (Workday/Ashby/LinkedIn often hide the real input behind a styled button).+ Refuses multi-file upload when the input lacks the `multiple` attribute.++ Args:+ instance_id (str): Browser instance ID.+ selector (str): CSS selector for the file input or any ancestor that contains it.+ paths (List[str]): Absolute local file paths to upload (single or multiple).++ Returns:+ Dict[str, Any]: { success: True, count: N, files: [basename, ...] }+ """+ tab = await browser_manager.get_tab(instance_id)+ if not tab:+ raise Exception(f"Instance not found: {instance_id}")+ return await dom_handler.file_upload(tab, selector, paths)+@section_tool("element-interaction")async def select_option(instance_id: str,