# SPDX-License-Identifier: Apache-2.0 """Download a file with the browser, then read it on the gateway and save a copy here. The browser saves downloads in the user's own folder on the gateway; wp.files lists them, extracts text or rows (PDF, DOCX, XLSX, CSV…) and streams them (resumable). export WEBPILOT_URL=http://localhost:8931 WEBPILOT_TOKEN=cbu_... python examples/fixture_site.py & python examples/python/download.py # the fixture's report.csv python examples/python/download.py https://example.org/files "Annual report" # any page and link text """ from __future__ import annotations import os import sys import time from webpilot import WebPilot FIXTURE = os.environ.get("WEBPILOT_FIXTURE", "http://host.docker.internal:8765") page = sys.argv[1] if len(sys.argv) > 1 else f"{FIXTURE}/files" link_text = sys.argv[2] if len(sys.argv) > 2 else "Download report" with WebPilot() as wp: before = {f.name for f in wp.files.list()} tab = wp.open(page, mode="act") try: link = tab.snapshot().first(link_text, role="link") tab.click(link.ref) for _ in range(30): # the download finishes in the background; wait until it is listed new = [f for f in wp.files.list() if f.name not in before] if new: break time.sleep(1) else: sys.exit("no new file after 30 s") f = new[0] print(f"downloaded {f.name} ({f.size} bytes, {f.mime})") if f.name.lower().endswith((".csv", ".xlsx", ".xls")): for row in list(wp.files.rows(f.name))[:5]: print(" ", row) else: print(wp.files.text(f.name).text[:500]) print("saved to", wp.files.save(f.name, ".")) wp.files.delete(f.name) # clean up on the gateway finally: tab.close()