Skip to content
Doppelganger logo

Understanding If Blocks

Conditional logic is essential for dynamic automation. Doppelganger provides If blocks to let your tasks make decisions based on selectors, variables, or CSV input.

An If block evaluates a condition and executes the actions inside once if the condition is true.
You must add an Else block (can be empty), and you must end the If/Else with an End Block to close the conditional scope.


1. When to Use If Blocks#

Use If blocks when you need to make decisions during automation, for example:

  • Click a button only if it exists
  • Extract data only if a value is present
  • Skip certain rows in a CSV if a variable matches a condition
  • Run alternative workflows based on a status variable

If your task depends on conditions rather than a fixed sequence, use an If block.


2. If Block Schema#

If blocks use a schema very similar to While blocks:

  • type"if"
  • selector → optional, element-based condition
  • value → optional, used with selector
  • conditionVar → optional, variable to evaluate
  • conditionVarType → string, number, boolean
  • conditionOp → equals, not equals, contains, exists, not exists
  • conditionValue → value to compare (used if variable is evaluated)

Key rules:

  • Only one condition type per If: either selector or variable
  • Else block is mandatory (can be empty)
  • End Block is required to close the If/Else scope
  • If executes once, not repeatedly like While

3. Example: Click Only If Button Exists With Else#

Goal: Click a Subscribe button only if it appears; otherwise log “No button.”

How to Set Up#

  1. Add an If block
    • selector: .subscribe-button
    • conditionOp: exists
  2. Add a Click block under the If
    • selector: .subscribe-button
  3. Add an Else block (mandatory)
    • Leave empty
  4. Add an End Block to close the If/Else scope

Execution:

  • If the button exists → Click runs
  • If the button does not exist → Else runs (even if empty)
  • End Block terminates the If/Else structure

4. Example: Conditional Logic Using Variables#

Goal: Only extract data if status == active, otherwise skip.

How to Set Up#

  1. Add an If block
    • conditionVar: status
    • conditionVarType: string
    • conditionOp: equals
    • conditionValue: active
  2. Add extraction or processing actions under the If block
  3. Add an Else block (can be empty) for skipped cases
  4. Add an End Block to close the conditional

Actions inside the If block run only when the variable meets the condition; Else runs when it does not.


5. Example: Skipping Rows in CSV#

If you’re using a CSV block as input:

  1. Add a For Each block for the CSV rows
  2. Add an If block inside the For Each to check a CSV variable, e.g., category != skip
  3. Add actions under the If block for rows that meet the condition
  4. Add an Else block (can be empty) for rows that do not meet the condition
  5. Add an End Block to close the If/Else scope

Only rows meeting the condition are processed; others run Else actions if defined.


6. Nested Ifs#

If blocks can be nested inside loops, While loops, or other If blocks.

Example workflow:

  • While selector exists .load-more
    • For Each row in CSV
      • If status == active
        → Extract data
      • Else (optional, can be empty)
        → Log “Skipped row”
      • End Block (closes If/Else)
    • End For Each
  • End While

Nested Ifs let you combine dynamic loops with conditional logic safely.


7. Best Practices#

  • Else block is mandatory, even if empty
  • End Block is required to close the If/Else structure
  • Use strong selectors or reliable variables
  • For CSV-based tasks, leverage variables from each row inside Ifs
  • Keep nested Ifs readable; break into separate tasks if logic is complex
  • Test your conditions to ensure actions trigger as intended

Summary#

If blocks allow your Doppelganger tasks to make decisions based on:

  • Selector state (exists / not exists)
  • Variable values
  • CSV row data

Combined with loops, mandatory Else blocks, and End Blocks, If blocks let you build dynamic, intelligent automation that reacts to page state, variables, and workflow logic.