Skip to content

API Reference parrot_tools.scraping.advanced_actions

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.scraping.advanced_actions

View source

Advanced action dispatch — Loop, Conditional, and template substitution.

Stateless async helpers extracted from the legacy WebScrapingTool so the modern executor, the legacy tool, and the FlowExecutor can all share a single implementation of Loop / Conditional dispatch (FEAT-222, Module 3).

The functions accept an :class:AbstractDriver plus a dispatch_step_fn callback for recursive step execution. This decouples them from any specific execution context (executor, tool, or flow engine).

The dispatch_step_fn callback signature mirrors executor._dispatch_step::

async def dispatch_step_fn(
    driver: AbstractDriver,
    step: ScrapingStep,
    url: str,
    timeout: int,
    step_extracted: Dict[str, Any],
) -> bool: ...

Alias AbstractDriver

Re-exported from parrot_tools.scraping.drivers.abstract.AbstractDriver.

Alias Conditional

Re-exported from parrot_tools.scraping.models.Conditional.

Attribute DispatchStepFn

Alias Loop

Re-exported from parrot_tools.scraping.models.Loop.

Alias ScrapingStep

Re-exported from parrot_tools.scraping.models.ScrapingStep.

Function exec_conditional

Execute a :class:Conditional action.

Evaluates the configured condition against the page and dispatches the actions_if_true or actions_if_false branch accordingly.

Supported condition_type values: exists, not_exists, text_contains, text_equals, attribute_equals (the latter expects expected_value formatted as "attr=value").

exec_conditional(driver: AbstractDriver, cond_action: Conditional, dispatch_step_fn: DispatchStepFn, base_url: str = '', timeout: int = 10) -> bool

Parameter Type Default Description
driver AbstractDriver Browser driver implementing :class:AbstractDriver.
cond_action Conditional The :class:Conditional model to evaluate.
dispatch_step_fn DispatchStepFn Callback dispatching a single step.
base_url str '' Base URL forwarded to each dispatched step.
timeout int 10 Default per-step timeout in seconds.

Returns: bool — True when the selected branch executed without failures (or there was no branch to run); False on an unknown condition type or when a dispatched sub-action failed.

Function exec_loop

Execute a :class:Loop action.

Supports fixed iteration counts, iteration over a values list with {value} substitution, JavaScript condition-controlled loops, break_on_error, the max_iterations safety limit, and the start_index offset.

exec_loop(driver: AbstractDriver, loop_action: Loop, dispatch_step_fn: DispatchStepFn, base_url: str = '', timeout: int = 10) -> bool

Parameter Type Default Description
driver AbstractDriver Browser driver implementing :class:AbstractDriver.
loop_action Loop The :class:Loop model to execute.
dispatch_step_fn DispatchStepFn Callback dispatching a single step (enables recursive loop-within-loop execution).
base_url str '' Base URL forwarded to each dispatched step.
timeout int 10 Default per-step timeout in seconds.

Returns: bool — True when the loop completed; False when it stopped early due to a failed step while break_on_error is set.

Attribute logger

Function substitute_template_vars

Recursively substitute loop template variables in value.

Supported tokens (inside {...}): - {i}, {index}, {iteration} — the current iteration, offset by start_index. - Arithmetic expressions — {i+1}, {i-1}, {i*2}, {index+1} — evaluated by a strict arithmetic-only AST parser (:func:_safe_arithmetic), never a general eval. - {value} (and {<value_name>}) — the current value when iterating over a values list.

Non-string scalars (int, bool, None) pass through unchanged; dicts and lists are walked recursively.

substitute_template_vars(value: Any, index: int, start_index: int = 0, values: Optional[List[Any]] = None, value_name: str = 'value', current_value: Any = None) -> Any

Parameter Type Default Description
value Any The value to substitute (str, dict, list, or scalar).
index int Current iteration counter (0-based).
start_index int 0 Offset applied to the exposed index (default 0).
values Optional[List[Any]] None Optional list being iterated over; values[index] becomes the current value.
value_name str 'value' Variable name exposed for the current value (default "value").
current_value Any None Explicit current value. When given it takes precedence over values[index] (lets callers pass a single value without allocating a positional list).

Returns: Any — The value with all template variables substituted.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally