- Applied automated linting suggestions from pre-commit hooks, primarily updating type hints (e.g., Dict -> dict) and organizing imports.
- Refactored Playwright script execution in examples/features/playwright_script_generation.py to use asyncio.create_subprocess_exec for asynchronous handling and real-time log streaming, resolving ASYNC221 lint error and improving output visibility.
Update imports in Playwright script generation and helper files to use `patchright` instead of `playwright`, aligning with current project dependencies.
Co-Authored-By: Nick Sweeting <511499+pirate@users.noreply.github.com>
Moved helper functions (replace_sensitive_data, _try_locate_and_act, PlaywrightActionError) from a multiline string within PlaywrightScriptGenerator into browser_use/agent/playwright_script_helpers.py.
This allows linters and type checkers to analyze the helper code, improving maintainability and code quality. The generator now reads the helper file content dynamically.
Co-Authored-By: Nick Sweeting <511499+pirate@users.noreply.github.com>
Remove comments automatically added by the LLM in agent-related files.
Removed comments from:
- browser_use/agent/playwright_script_generator.py
- browser_use/agent/service.py
- browser_use/agent/views.py
These comments were redundant and cluttered the code.
Refactored the `_map_action_to_playwright` method in `PlaywrightScriptGenerator` to address its excessive length and complexity stemming from numerous if/elif blocks.
Introduced a dictionary (`_action_handlers`) to map action type strings to dedicated private handler methods (e.g., `_map_go_to_url`, `_map_input_text`). The main `_map_action_to_playwright` method now delegates the call to the appropriate handler via this dictionary lookup.
This change significantly simplifies the main mapping logic, improves code readability, isolates action-specific logic, and makes the overall structure easier to maintain and extend. It directly resolves the concern about the method's length and error-proneness.
Implement the ability to generate executable Playwright scripts from an Agent's run history (`AgentHistoryList`).
This introduces the `PlaywrightScriptGenerator` class which translates recorded agent actions (clicks, inputs, navigation, etc.) into a Python script using Playwright's async API.
Key features:
- Uses XPath selectors derived from the agent's interaction history.
- Includes a robust helper function (`_try_locate_and_act`) with XPath prefix trimming for improved resilience on dynamic pages.
- Handles sensitive data placeholders by mapping them to environment variables in the generated script.
- Supports configuring the browser launch and context based on the original agent's `BrowserConfig` and `BrowserContextConfig`.
- Activated by setting the `save_playwright_script_path` option in `AgentSettings`.
Includes modifications to `Agent` service to trigger generation and `AgentHistoryList` view to add the saving method. Also adds a new example (`examples/features/playwright_script_generation.py`).
This feature enables reliable replay of agent runs, automated testing, and scripting of repetitive tasks discovered by the agent.