Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New download action #303

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions browsergym/core/src/browsergym/core/action/functions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -622,3 +624,41 @@ 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, 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...")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is useful for debugging but should be removed once your code is working. Please remove these.


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")
Comment on lines +653 to +658
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. This for loop is not needed in the end, the save_as call will wait for the download to complete.


# Save the file to the specified path
download.save_as(file_path)

# Notify the user about download completion
print(f"Download completed: {file_path}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed.

2 changes: 2 additions & 0 deletions browsergym/core/src/browsergym/core/action/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
tab_close,
tab_focus,
upload_file,
download_file,
)
from .parsers import action_docstring_parser, highlevel_action_parser

Expand All @@ -59,6 +60,7 @@
clear,
drag_and_drop,
upload_file,
download_file,
],
"coord": [
scroll,
Expand Down
Loading