mirror of
https://github.com/browser-use/browser-use
synced 2026-05-06 17:52:15 +02:00
remove the default save_pdf option
This commit is contained in:
@@ -2,7 +2,6 @@ import asyncio
|
||||
import enum
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import Any, Generic, TypeVar, cast
|
||||
|
||||
@@ -245,21 +244,6 @@ class Controller(Generic[Context]):
|
||||
long_term_memory=f"Input '{params.text}' into element {params.index}.",
|
||||
)
|
||||
|
||||
# Save PDF
|
||||
@self.registry.action('Save the current page as a PDF file')
|
||||
async def save_pdf(page: Page):
|
||||
short_url = re.sub(r'^https?://(?:www\.)?|/$', '', page.url)
|
||||
slug = re.sub(r'[^a-zA-Z0-9]+', '-', short_url).strip('-').lower()
|
||||
sanitized_filename = f'{slug}.pdf'
|
||||
|
||||
await page.emulate_media(media='screen')
|
||||
await page.pdf(path=sanitized_filename, format='A4', print_background=False)
|
||||
msg = f'Saving page with URL {page.url} as PDF to ./{sanitized_filename}'
|
||||
logger.info(msg)
|
||||
return ActionResult(
|
||||
extracted_content=msg, include_in_memory=True, long_term_memory=f'Saved PDF to {sanitized_filename}'
|
||||
)
|
||||
|
||||
# Tab Management Actions
|
||||
@self.registry.action('Switch tab', param_model=SwitchTabAction)
|
||||
async def switch_tab(params: SwitchTabAction, browser_session: BrowserSession):
|
||||
|
||||
55
examples/custom-functions/save_pdf.py
Normal file
55
examples/custom-functions/save_pdf.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import asyncio
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
from browser_use import ActionResult, Agent, Controller
|
||||
from browser_use.browser.types import Page
|
||||
from browser_use.llm import ChatOpenAI
|
||||
|
||||
# Initialize controller
|
||||
controller = Controller()
|
||||
|
||||
download_path = Path.cwd() / 'downloads'
|
||||
|
||||
|
||||
# Save PDF - exact copy from original controller function
|
||||
@controller.registry.action('Save the current page as a PDF file')
|
||||
async def save_pdf(page: Page):
|
||||
short_url = re.sub(r'^https?://(?:www\.)?|/$', '', page.url)
|
||||
slug = re.sub(r'[^a-zA-Z0-9]+', '-', short_url).strip('-').lower()
|
||||
sanitized_filename = f'{slug}.pdf'
|
||||
|
||||
await page.emulate_media(media='screen')
|
||||
await page.pdf(path=download_path / sanitized_filename, format='A4', print_background=False)
|
||||
msg = f'Saving page with URL {page.url} as PDF to {download_path / sanitized_filename}'
|
||||
return ActionResult(extracted_content=msg, include_in_memory=True, long_term_memory=f'Saved PDF to {sanitized_filename}')
|
||||
|
||||
|
||||
async def main():
|
||||
"""
|
||||
Example task: Navigate to browser-use.com and save the page as a PDF
|
||||
"""
|
||||
task = """
|
||||
Go to https://browser-use.com/ and save the page as a PDF file.
|
||||
"""
|
||||
|
||||
# Initialize the language model
|
||||
model = ChatOpenAI(model='gpt-4.1-mini')
|
||||
|
||||
# Create and run the agent
|
||||
agent = Agent(task=task, llm=model, controller=controller)
|
||||
|
||||
result = await agent.run()
|
||||
print(f'🎯 Task completed: {result}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user