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
Refactor file upload process in file_upload.py: Removed BrowserProfile initialization, streamlined BrowserSession usage, and updated task description for clarity.
Magnus Müllerba7ac0c9d2f8
diff --git a/examples/custom-functions/file_upload.py b/examples/custom-functions/file_upload.pyindex 65dd8a5fa..858cdd7f5 100644--- a/examples/custom-functions/file_upload.py+++ b/examples/custom-functions/file_upload.py@@ -15,15 +15,10 @@ from langchain_openai import ChatOpenAIfrom browser_use import Agent, Controllerfrom browser_use.agent.views import ActionResult-from browser_use.browser import BrowserProfile, BrowserSession+from browser_use.browser import BrowserSessionlogger = logging.getLogger(__name__)-# Initialize browser and controller-browser_profile = BrowserProfile(- headless=False,- executable_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',-)controller = Controller()@@ -37,7 +32,7 @@ async def upload_file(index: int, path: str, browser_session: BrowserSession, avif not os.path.exists(path):return ActionResult(error=f'File {path} does not exist')- file_upload_dom_el = await browser_session.find_file_upload_element_by_index(index)+ file_upload_dom_el = await browser_session.find_file_upload_element_by_index(index, max_height=3, max_descendant_depth=3)if file_upload_dom_el is None:msg = f'No file upload element found at index {index}'@@ -83,25 +78,21 @@ def create_file(file_type: str = 'txt'):async def main():+ 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://kzmpmkh2zfk1ojnpxfn1.lite.vusercontent.net/ and - read the file content and upload them to fields'available_file_paths = [create_file('txt'), create_file('pdf'), create_file('csv')]model = ChatOpenAI(model='gpt-4o')- browser_session = BrowserSession(browser_profile=browser_profile)- await browser_session.start()agent = Agent(task=task,llm=model,controller=controller,- browser_session=browser_session,available_file_paths=available_file_paths,)await agent.run()- await browser_session.stop()-input('Press Enter to close...')