mirror of
https://github.com/browser-use/browser-use.git
synced 2025-02-18 01:18:20 +03:00
Ability to read files
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -181,3 +181,6 @@ gcp-login.json
|
|||||||
.vscode
|
.vscode
|
||||||
.ruff_cache
|
.ruff_cache
|
||||||
.idea
|
.idea
|
||||||
|
*.txt
|
||||||
|
*.pdf
|
||||||
|
*.csv
|
||||||
@@ -1049,7 +1049,7 @@ class BrowserContext:
|
|||||||
element_handle = await self.get_locate_element(selector_map[index])
|
element_handle = await self.get_locate_element(selector_map[index])
|
||||||
return element_handle
|
return element_handle
|
||||||
|
|
||||||
async def get_dom_element_by_index(self, index: int) -> DOMElementNode | None:
|
async def get_dom_element_by_index(self, index: int) -> DOMElementNode:
|
||||||
selector_map = await self.get_selector_map()
|
selector_map = await self.get_selector_map()
|
||||||
return selector_map[index]
|
return selector_map[index]
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ controller = Controller()
|
|||||||
|
|
||||||
|
|
||||||
@controller.action(
|
@controller.action(
|
||||||
'Upload file to element ',
|
'Upload file to interactive element with file path ',
|
||||||
)
|
)
|
||||||
async def upload_file(index: int, path: str, browser: BrowserContext, available_file_paths: list[str]):
|
async def upload_file(index: int, path: str, browser: BrowserContext, available_file_paths: list[str]):
|
||||||
if path not in available_file_paths:
|
if path not in available_file_paths:
|
||||||
@@ -38,47 +38,53 @@ async def upload_file(index: int, path: str, browser: BrowserContext, available_
|
|||||||
|
|
||||||
dom_el = await browser.get_dom_element_by_index(index)
|
dom_el = await browser.get_dom_element_by_index(index)
|
||||||
|
|
||||||
if dom_el is None:
|
|
||||||
return ActionResult(error=f'No element found at index {index}')
|
|
||||||
|
|
||||||
file_upload_dom_el = dom_el.get_file_upload_element()
|
file_upload_dom_el = dom_el.get_file_upload_element()
|
||||||
|
|
||||||
if file_upload_dom_el is None:
|
if file_upload_dom_el is None:
|
||||||
logger.info(f'No file upload element found at index {index}')
|
msg = f'No file upload element found at index {index}'
|
||||||
return ActionResult(error=f'No file upload element found at index {index}')
|
logger.info(msg)
|
||||||
|
return ActionResult(error=msg)
|
||||||
|
|
||||||
file_upload_el = await browser.get_locate_element(file_upload_dom_el)
|
file_upload_el = await browser.get_locate_element(file_upload_dom_el)
|
||||||
|
|
||||||
if file_upload_el is None:
|
if file_upload_el is None:
|
||||||
logger.info(f'No file upload element found at index {index}')
|
msg = f'No file upload element found at index {index}'
|
||||||
return ActionResult(error=f'No file upload element found at index {index}')
|
logger.info(msg)
|
||||||
|
return ActionResult(error=msg)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await file_upload_el.set_input_files(path)
|
await file_upload_el.set_input_files(path)
|
||||||
msg = f'Successfully uploaded file to index {index}'
|
msg = f'Successfully uploaded file to index {index}'
|
||||||
logger.info(msg)
|
logger.info(msg)
|
||||||
return ActionResult(extracted_content=msg)
|
return ActionResult(extracted_content=msg, include_in_memory=True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug(f'Error in set_input_files: {str(e)}')
|
msg = f'Failed to upload file to index {index}: {str(e)}'
|
||||||
return ActionResult(error=f'Failed to upload file to index {index}')
|
logger.info(msg)
|
||||||
|
return ActionResult(error=msg)
|
||||||
|
|
||||||
|
|
||||||
@controller.action('Close file dialog')
|
@controller.action('Read the file content of a file given a path')
|
||||||
async def close_file_dialog(browser: BrowserContext):
|
async def read_file(path: str, available_file_paths: list[str]):
|
||||||
page = await browser.get_current_page()
|
if path not in available_file_paths:
|
||||||
await page.keyboard.press('Escape')
|
return ActionResult(error=f'File path {path} is not available')
|
||||||
|
|
||||||
|
with open(path, 'r') as f:
|
||||||
|
content = 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'):
|
def create_file(file_type: str = 'txt'):
|
||||||
with open(f'tmp.{file_type}', 'w') as f:
|
with open(f'tmp.{file_type}', 'w') as f:
|
||||||
f.write('test')
|
f.write('test')
|
||||||
file_path = Path.cwd() / f'tmp.{file_type}'
|
file_path = Path.cwd() / f'tmp.{file_type}'
|
||||||
print(f'Created file: {file_path}')
|
logger.info(f'Created file: {file_path}')
|
||||||
return str(file_path)
|
return str(file_path)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
task = f'Go to https://kzmpmkh2zfk1ojnpxfn1.lite.vusercontent.net/ and upload one file to each upload field'
|
task = f'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')]
|
available_file_paths = [create_file('txt'), create_file('pdf'), create_file('csv')]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user