Nova Act supports file uploads and downloads through a combination of natural language commands and Playwright APIs. For security, file operations require explicit permission via SecurityOptions.
import osfrom nova_act import NovaAct, SecurityOptionsdef upload_file_safely(filepath: str, upload_url: str): """Upload a file with validation.""" # Validate file exists if not os.path.exists(filepath): raise FileNotFoundError(f"File not found: {filepath}") # Validate file size max_size = 10 * 1024 * 1024 # 10MB if os.path.getsize(filepath) > max_size: raise ValueError(f"File too large: {filepath}") # Get directory for security options file_dir = os.path.dirname(filepath) security_options = SecurityOptions( allowed_file_upload_paths=[f"{file_dir}/*"] ) with NovaAct( starting_page=upload_url, security_options=security_options ) as nova: # Navigate to upload form nova.act("click the upload button to open the file dialog") # Upload the file nova.act(f"select and upload {filepath}") # Wait for confirmation result = nova.act_get("check if upload was successful") return result.response# Usageresult = upload_file_safely( "/uploads/report.pdf", "https://example.com/upload")print(f"Upload result: {result}")
from nova_act import NovaActwith NovaAct(starting_page="https://example.com/downloads") as nova: # Capture download with nova.page.expect_download() as download_info: nova.act("click on the download button") # Get temporary path temp_path = download_info.value.path() print(f"Downloaded to temporary path: {temp_path}") # Save permanently download_info.value.save_as("/downloads/my_file.pdf") print("File saved permanently")
Important notes:
The browser downloads to a temporary path managed by Playwright
Access temporary path via download_info.value.path()
Use save_as() to save permanently:
With full path: /path/to/my_file.pdf
With filename only: my_file.pdf (saves to current working directory)
from nova_act import NovaActwith NovaAct(starting_page="https://example.com/page") as nova: # Get rendered HTML html_content = nova.page.content() # Save to file with open("/downloads/page.html", "w", encoding="utf-8") as f: f.write(html_content)
from nova_act import NovaActwith NovaAct(starting_page="https://example.com/document.pdf") as nova: # Download current page content response = nova.page.request.get(nova.page.url) # Save to file with open("/downloads/document.pdf", "wb") as f: f.write(response.body())
def handle_confirm(dialog): """Handle confirm dialog with conditional logic.""" print(f"Confirm dialog: {dialog.message}") # Accept if message contains certain text if "Are you sure" in dialog.message: dialog.accept() else: dialog.dismiss()with NovaAct(starting_page="https://example.com") as nova: nova.page.on("dialog", handle_confirm) nova.act("submit the form") nova.page.remove_listener("dialog", handle_confirm)
def handle_prompt(dialog): """Handle prompt dialog with input.""" print(f"Prompt: {dialog.message}") # Provide input text dialog.accept("My Input Value")with NovaAct(starting_page="https://example.com") as nova: nova.page.on("dialog", handle_prompt) nova.act("click the button that asks for my name") nova.page.remove_listener("dialog", handle_prompt)
from nova_act import NovaAct, SecurityOptionsupload_path = "/uploads/document.pdf"security_options = SecurityOptions( allowed_file_upload_paths=["/uploads/*"])with NovaAct( starting_page="https://example.com/upload", security_options=security_options) as nova: # Nova Act handles file dialogs automatically when you specify a filename nova.act(f"upload {upload_path}") # Or use Playwright directly for more control with nova.page.expect_file_chooser() as fc_info: nova.act("click the upload button") file_chooser = fc_info.value file_chooser.set_files(upload_path)
def validate_file(filepath: str) -> bool: """Validate file before upload.""" # Check existence if not os.path.exists(filepath): return False # Check size (max 10MB) if os.path.getsize(filepath) > 10 * 1024 * 1024: return False # Check extension allowed_extensions = [".pdf", ".txt", ".csv"] if not any(filepath.endswith(ext) for ext in allowed_extensions): return False return True
Use Temporary Directories
Use temporary directories for staging:
import tempfileimport shutilwith tempfile.TemporaryDirectory() as temp_dir: # Copy file to temp location temp_file = os.path.join(temp_dir, "file.pdf") shutil.copy("/source/file.pdf", temp_file) # Upload from temp directory security_options = SecurityOptions( allowed_file_upload_paths=[f"{temp_dir}/*"] ) # ... upload ...# Temp directory automatically cleaned up
Handle Download Failures
Always handle download failures gracefully:
try: with nova.page.expect_download(timeout=30000) as download_info: nova.act("click download") download_info.value.save_as(save_path) if not os.path.exists(save_path): raise Exception("Download file not found")except Exception as e: print(f"Download failed: {e}") # Retry or handle error
Clean Up Downloads
Clean up downloaded files when done:
download_path = "/downloads/temp_file.pdf"try: # Use the file process_file(download_path)finally: # Clean up if os.path.exists(download_path): os.remove(download_path)
import tempfileimport osfrom nova_act import NovaAct, SecurityOptionsdef process_file_sandboxed(input_file: str, website_url: str): """Process file in sandboxed environment.""" with tempfile.TemporaryDirectory() as sandbox: # Copy input to sandbox sandbox_input = os.path.join(sandbox, os.path.basename(input_file)) shutil.copy(input_file, sandbox_input) # Configure security for sandbox only security_options = SecurityOptions( allowed_file_upload_paths=[f"{sandbox}/*"], allowed_file_open_paths=[f"{sandbox}/*"] ) with NovaAct( starting_page=website_url, security_options=security_options ) as nova: # Upload from sandbox nova.act(f"upload {sandbox_input}") # Download result to sandbox with nova.page.expect_download() as download_info: nova.act("download the processed file") sandbox_output = os.path.join(sandbox, "output.pdf") download_info.value.save_as(sandbox_output) # Copy result out of sandbox shutil.copy(sandbox_output, "/output/processed.pdf") # Sandbox automatically cleaned up