Skip to content

Downloads example doesn't work #108

Closed

Description

@jimgoo

I was trying to run the downloads example here for the MP3: https://docs.browserbase.com/features/screenshots-downloads-uploads#downloads, but my zip file is always empty. Copying the code below for reference.

import os
import requests
from playwright.sync_api import sync_playwright, Page
from browserbase import Browserbase

API_KEY = os.environ["BROWSERBASE_API_KEY"]
PROJECT_ID = os.environ["BROWSERBASE_PROJECT_ID"]

# createSession()

def get_zipped_downloads(session_id: str) -> bytes:
    """
    Get a zipped archive of the files that were downloaded during the session.

    :returns: the zipped file data
    """
    session_url = (
        f"https://api.browserbase.com/v1/sessions/{session_id}/downloads"
    )
    headers = {"x-bb-api-key": API_KEY}
    response = requests.get(session_url, headers=headers)

    # Raise an exception if there wasn't a good response from the endpoint
    response.raise_for_status()
    return response.content

def run(browser_tab: Page): # Head to the test downloads page
    browser_tab.goto(
        "https://browser-tests-alpha.vercel.app/api/download-test"
    )

    # Click the download button
    print("Downloading the MP3 file to the remote browser...")
    browser_tab.get_by_role("link", name="Download File").click()

browserbase = Browserbase(api_key=os.environ["BROWSERBASE_API_KEY"])
session = browserbase.sessions.create(project_id=os.environ["BROWSERBASE_PROJECT_ID"], proxies=True)
#session_id = create_session()
session_id = session.id
print(f"{session_id=}")

with sync_playwright() as playwright: # Use the session ID to connect to the browser
    browser = playwright.chromium.connect_over_cdp(f"wss://connect.browserbase.com?apiKey={API_KEY}&sessionId={session_id}")

    # Required to avoid playwright overriding the location
    session = browser.new_browser_cdp_session()
    session.send(
        "Browser.setDownloadBehavior",
        {
            "behavior": "allow",
            "downloadPath": "downloads",
            "eventsEnabled": True,
        },
    )

    # Print a bit of info about the browser we've connected to
    print(
        "Connected to Browserbase.",
        f"{browser.browser_type.name} version {browser.version}",
    )

    context = browser.contexts[0]
    browser_tab = context.pages[0]

    try:
        # Perform our browser commands
        run(browser_tab)

    finally:
        # Clean up
        browser_tab.close()
        browser.close()

print(
"Retrieving files that were downloaded to the remote browser during the session..."
)
zipped_downloads = get_zipped_downloads(session_id)
with open("downloads.zip", "wb") as file:
    file.write(zipped_downloads)

print("Downloaded files are in the downloads.zip file.")

Activity

  1. manojsundayai commented on Mar 6, 2025

    @manojsundayai

    Same problem for me as well. Wondering if their storage doesn't work anymore.

    It strangely worked a couple of times for me and then it stopped working,.

  2. jimgoo commented on Mar 6, 2025

    @jimgoo
    Author

    The advice I got over email was to put a 3 second sleep after clicking the button to give it time to download, which worked.

  3. manojsundayai commented on Mar 6, 2025

    @manojsundayai

    Actually nvm, mine works. The problem was I replaced '"downloadPath": "downloads",' line with something else. I changed it back to just "downloads" and it works very well.

    Thanks @jimgoo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions