From 08222d45137e22fee1f528fcbc2b651b74c013fd Mon Sep 17 00:00:00 2001 From: RohitP2005 Date: Sat, 11 Jan 2025 11:45:12 +0530 Subject: [PATCH 1/2] Intial commit --- .../src/browsergym/core/action/functions.py | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/browsergym/core/src/browsergym/core/action/functions.py b/browsergym/core/src/browsergym/core/action/functions.py index bb31db9a..07afc01a 100644 --- a/browsergym/core/src/browsergym/core/action/functions.py +++ b/browsergym/core/src/browsergym/core/action/functions.py @@ -1,5 +1,7 @@ # these are placeholders # all these symbols will be available in browsergym actions +import os +import time from typing import Literal import playwright.sync_api @@ -622,3 +624,66 @@ def mouse_upload_file(x: float, y: float, file: str | list[str]): file_chooser = fc_info.value file_chooser.set_files(file) + + +def download_file(bid: str, save_path: str): + """ + Click an element to trigger a file download and save it to a specified path. + + Parameters: + bid (str): The identifier of the element to click. + save_path (str): The full path where the downloaded file should be saved. + + Examples: + download_file("572", "/path/to/save/receipt.pdf") + """ + elem = get_elem_by_bid(page, bid, demo_mode != "off") + add_demo_mode_effects(page, elem, bid, demo_mode=demo_mode, move_cursor=True) + + # Listen for the download event + with page.expect_download() as download_info: + elem.click(timeout=500) # Click the element to start the download + + download = download_info.value + # Save the downloaded file to the specified path + download.save_as(save_path) + + print(f"File downloaded and saved at {save_path}") + + +def download_file(bid: str, download_path: str = "downloads/"): + """ + Initiates a download, updates the user on the progress, and notifies on completion. + + Args: + bid (str): The ID of the element to click to initiate the download. + download_path (str): The directory where the downloaded file will be saved. + """ + # Ensure the download directory exists + if not os.path.exists(download_path): + os.makedirs(download_path) + + # Initiate the download process + elem = get_elem_by_bid(page, bid, demo_mode != "off") + add_demo_mode_effects(page, elem, bid, demo_mode=demo_mode, move_cursor=True) + + print("Starting download...") + + with page.expect_download() as download_info: + elem.click(timeout=500) + + download = download_info.value + file_path = os.path.join(download_path, download.suggested_filename) + + # Monitor the download progress + start_time = time.time() + while not download.is_done(): + time.sleep(1) + elapsed_time = time.time() - start_time + print(f"Downloading... Elapsed time: {int(elapsed_time)} seconds") + + # Save the file to the specified path + download.save_as(file_path) + + # Notify the user about download completion + print(f"Download completed: {file_path}") From ce1f73c320365c3eb5a10adf1520d5782a263452 Mon Sep 17 00:00:00 2001 From: RohitP2005 Date: Sat, 11 Jan 2025 12:07:45 +0530 Subject: [PATCH 2/2] Download method added --- .../src/browsergym/core/action/functions.py | 25 ------------------- .../src/browsergym/core/action/highlevel.py | 2 ++ 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/browsergym/core/src/browsergym/core/action/functions.py b/browsergym/core/src/browsergym/core/action/functions.py index 07afc01a..b1b27a7a 100644 --- a/browsergym/core/src/browsergym/core/action/functions.py +++ b/browsergym/core/src/browsergym/core/action/functions.py @@ -626,31 +626,6 @@ def mouse_upload_file(x: float, y: float, file: str | list[str]): file_chooser.set_files(file) -def download_file(bid: str, save_path: str): - """ - Click an element to trigger a file download and save it to a specified path. - - Parameters: - bid (str): The identifier of the element to click. - save_path (str): The full path where the downloaded file should be saved. - - Examples: - download_file("572", "/path/to/save/receipt.pdf") - """ - elem = get_elem_by_bid(page, bid, demo_mode != "off") - add_demo_mode_effects(page, elem, bid, demo_mode=demo_mode, move_cursor=True) - - # Listen for the download event - with page.expect_download() as download_info: - elem.click(timeout=500) # Click the element to start the download - - download = download_info.value - # Save the downloaded file to the specified path - download.save_as(save_path) - - print(f"File downloaded and saved at {save_path}") - - def download_file(bid: str, download_path: str = "downloads/"): """ Initiates a download, updates the user on the progress, and notifies on completion. diff --git a/browsergym/core/src/browsergym/core/action/highlevel.py b/browsergym/core/src/browsergym/core/action/highlevel.py index da2c539c..e5566dcf 100644 --- a/browsergym/core/src/browsergym/core/action/highlevel.py +++ b/browsergym/core/src/browsergym/core/action/highlevel.py @@ -38,6 +38,7 @@ tab_close, tab_focus, upload_file, + download_file, ) from .parsers import action_docstring_parser, highlevel_action_parser @@ -59,6 +60,7 @@ clear, drag_and_drop, upload_file, + download_file, ], "coord": [ scroll,