Macro: Record and Replay Automated Actions
The Starter plan and above include Macros.
What is a Macro in Distill?
Macros allow you to record a sequence of actions performed on a web page. Some examples of webpage actions are scrolling, button clicks, or form submissions on the page. Once you record the Macro, you can replay it on the webpage.
In Distill, you can use Macros in two ways:
- For monitoring the desired section of the webpage, where the content displays only after performing web page actions (button click, scroll, etc.).
- As a Macro action after Distill checks the web page for changes. When Distill detects a change in your monitor, the Macro will replay the actions on the web page.
Use macros when the page URL doesn’t change after actions like submitting forms, sorting, filtering, logging in, or searching. Macros can replicate these actions, taking you to the desired page view.
You will need to follow two steps for monitoring webpages that need Macro:
- Create a Macro that records the flow of actions on the web page.
- Use the saved Macro and play it before monitoring the section on the web page.
Here’s a video tutorial with an example showcasing how to create a macro, record actions, and use it on a webpage monitor.
How to create a Macro?
You can create Macros from the web app or the browser extension.
- Click on the Macros option from the hamburger icon ☰. This opens your macro list.
-
Enter a name in New Macro Name and the page URL in Start URL, then click Add New Macro. This opens the URL in Distill’s remote browser.
-
Once the page loads, perform the web page actions to display the final content. You will also see the corresponding steps for each action in the right panel, under steps, as shown below.
-
[Optional] Click the Play button (play icon) to ensure that you have captured the correct flow of actions.
-
Click the Save button to save the Macro.
The Macro list opens after saving a Macro and shows the newly added Macro.
Record on a dedicated cloud device
Add New Macro always opens the recorder in Distill’s cloud. To record on one of your dedicated cloud devices instead:
- Create the Macro as above, or pick an existing one in your macro list.
- Click the dropdown arrow next to Open Recorder Using and select the device.
The recorder opens in that device’s browser. The Proxy and Profile menus show only when you record in the cloud. The next time, Open Recorder Using opens the device you picked last.
How to use a Macro for monitoring?
You can use a Macro to make selections on a page for monitoring. This will work for both cloud and local monitors. To use it for a local monitor, you will have to change the device from the Option page or the Watchlist.
Here are the steps to use a Macro:
-
In the Watchlist, click Add Monitor → Webpage. Enter the URL and click Monitor Parts of page. If the button shows another option, pick Parts of page from the dropdown next to it first.
-
From the visual selector toolbar, click the Macro - <None> menu. This shows all the saved Macros for the URL. Select the Macro from the drop-down list. The menu then shows the Macro’s number of steps and name.
-
Click the play button (Play Macro) left of the Macro’s name. This plays the recorded actions and reaches the final page where you can make selections to monitor.
-
Once the Macro finishes playing, click Select elements at the top of the right panel and select the elements to monitor. Save the selections.
-
Configure the settings like check interval, device, and others from the Options page and click Save.
How to use Macro actions after checks?
Macro actions are available for local monitors.
You can set up Macro actions to replay a set of steps on a page once your monitor detects a change.
- Create a Macro as explained above to record the actions to be performed after the change is detected.
- Create a monitor for that web page by clicking Add Monitor → Webpage in the Watchlist.
- In the options page, click on Add actions and select Run Macro (for local monitor) from the actions list.
Some common scenarios when you can use macro actions are:
- Automate adding items to cart when its available: You can set up a monitor to track a sold-out product. When the visual selector detects a change from “Sold Out” to “Add to Cart” on the product page, use Macro action to automate adding the item to your cart. It will open the page in a new tab and replay the actions recorded in the Macro.
Here’s a video tutorial with instructions to use macro actions:
-
Applying to a job opening: While tracking a careers page, if a role starts accepting applications, you can use Macro actions to apply for the job automatically.
-
Auto login: While monitoring pages that are accessible only when logged in, use a conditional block in a macro to check if the page displays a login form. If the login form is detected, the macro action will automatically log in to the page.
How to edit a Macro?
You can edit, add, or delete a step for an existing Macro.
- Open your macro list.
- Click Open Recorder Using for the Macro that you need to edit. The button ends with the device name, such as the cloud or a dedicated cloud device.
- Click the overflow icon (⋮) as shown below for a step and edit as needed. This opens a menu with options.
-
The menu shows only the options that apply to that step:
- Edit: Replace Statement (Replace Value on a value), Insert After, Insert Before, Insert At End (on a block of steps), Duplicate and Add else if after this (on an if line), Add Arguments or Add Optional Fields (when some are unset), Create Input Field (on a value), and Edit Source.
- Play: Play Step, Play Until, and Set Breakpoint or Remove Breakpoint. See Play and debug steps.
- Record: Start recording after this step, or Start recording inside a condition or loop.
- Delete removes the step.
-
To change a value, such as a step’s selector, click it. Edit it in the box that opens and click Save.
Saved edits will apply to all the monitors using this Macro.
To edit the steps without opening a browser, click the Macro’s N steps link in the macro list. This opens Edit Macro with the same steps and ⋮ menus. Its menus have only the Edit and Delete options: playing and recording need the recorder.
Play and debug steps
The recorder can play the whole Macro or a part of it, so you can find the step that fails.
- Play (play icon above the steps) plays the Macro from the start in a new tab. While it plays, use Pause and Stop.
- Play Step in a step’s ⋮ menu plays only that step.
- Play Until in a step’s ⋮ menu plays the Macro from the start and pauses at that step.
- Set Breakpoint in a step’s ⋮ menu makes playback pause at that step. Remove Breakpoint clears it. To clear all breakpoints, click the ⋯ button next to Stop and select Clear Breakpoints.
When playback is paused, click Step to play the next step, or Play to continue.
Get help from the AI assistant
The Macro recorder has an AI assistant that helps you create Macros and fix errors. It can read the Macro’s steps, add, change or delete steps, and play them in the recorder’s browser to check the result.
- Open the recorder for a Macro.
- Click Ask AI at the bottom of the recorder. The chat opens in a corner of the window. Click Dock in the chat to place it beside the steps.
- Describe what the Macro should do, or ask the assistant to fix a step that fails.
- Check the steps it changed, then click Save to keep them.
What are conditions in Macros?
Within the steps in the Macro, you can add a Conditional block (IF…ELSE). It allows you to execute specific steps based on whether a condition evaluates to true or false. It is similar to IF…ELSE structure in programming languages.
You can leverage Macro conditional blocks in several scenarios, including but not limited to:
- Managing Intermittent Pop-ups: You can use conditional blocks to handle pop-ups that appear sporadically. If a pop-up is detected, the macro executes a specified set of steps; otherwise, it continues with an alternate sequence.
- Ensuring Continuous Access: For pages requiring authentication, conditional blocks can automate the login process. This is especially useful if you’re monitoring a page and find yourself logged out.
Specifically, for local monitors tracking access to password-protected pages, you may encounter session timeouts that result in being logged out. In these instances, you can insert an IF…ELSE conditional block within your macro to check the login status. If logged out, the macro can automatically execute steps to log back in, preventing errors and ensuring continuous monitoring.
How to add conditions in Macro steps?
Steps to add if…else in the Macro recorder:
- Click the ⋮ menu of the step where you want to add a condition and open Insert After or Insert Before. Under Control Flow, select Conditional (if/else).
- A
condblock is added. Click the arrow next tocondto expand it. It has anifline with aconditionand abody. In the example below, the condition checks whether the page has a.newsletter-popupelement.
- To change the condition, click the ⋮ menu on the
conditionrow and select Replace Value. To combine it with more conditions, select and, or or not under Surround with in the same menu. To add steps to the body, select Insert At End in the ⋮ menu ofbody, or Insert After in the ⋮ menu of a step in the body.
There is a provision to “Add clause” in Macro steps. This gives you the ability to check for multiple conditions when each condition leads to a different outcome of steps. Follow these steps to add a clause with an ELSE…IF
- Click the ⋮ menu on the if (or else if) line and select Add else if after this.
- An
else ifline with its ownconditionandbodyis added. Its body runs when the conditions above it are false and its own condition is true. In the example below, the Macro closes the newsletter pop-up if it shows, or else picks a region if the region picker shows.
How to use Assert expression in the Macro recorder?
Assert expression is used for error-checking during the macros execution.
- Use assert to verify the webpage’s expected behavior when the macro steps are being executed.
- If an assertion fails due to any missing response to the step in the macro, it will raise an exception to halt execution of next steps.
- Assert will error out the monitor with the error code set in the step,
E_ASSERTby default.
For example, verifying if a page exists before beginning to check any section inside it.
For this, add an assert step whose condition checks for an element that only the real page has. In the example below, the condition is element_exists #reviews. If the page has no #reviews element, the assert fails with the error code E_ASSERT and the message “Page does not exist”.
When you run the monitor, and page is not found, the macro will assert an error and stop further checks from executing.
What are Loops in Macros?
Loops in macros allow you to automate repetitive tasks by repeatedly executing a block of actions. There are two primary types of loops that you can use in Distill macros: For Loops and While Loops. Each serves different purposes depending on the nature of the repetition and the conditions under which the loop should run.
For Loops
For loops are used to iterate over a set of values or elements. They are particularly useful when you need to perform an action a specific number of times or over an array of elements. In Distill, you can use For Loops in several ways:
- Iterate over elements: If a webpage has multiple similar elements (like items in a list or tags), you can set a For Loop to perform actions on each element one by one.
- Iterate using a range: You can specify a range with the
range()function to execute the loop a certain number of times. This is handy for tasks such as clicking a button multiple times or navigating through pagination by incrementing page numbers.
While Loops
While Loops evaluate a condition before each iteration, and the loop continues until the condition is no longer true. This type of loop is ideal for situations where the number of iterations isn’t known in advance and must be determined dynamically. For example, you might use a While Loop to continue clicking a “Load More” button until it no longer appears on a webpage.
How to add Loops in Macro steps
Steps to add a While Loop in the Macro recorder:
-
Click the ⋮ menu of the step where you want the loop and open Insert After or Insert Before. Under Control Flow, select While Loop.
-
A
whileblock is added. Click the arrow next towhileto expand it. It has aconditionand abody. Set the condition and add steps to the body as for a conditional block.
Steps to add For loop in the Macro recorder:
-
Click the ⋮ menu of the step where you want the loop and open Insert After or Insert Before. Under Control Flow, select For Loop.
-
A
forblock is added. Click the arrow next toforto expand it. It has avariable, aniterableand abody. -
To execute the loop a predetermined number of times, utilize the
rangefunction. For example, to repeat an action five times, userange(5). This sets the loop to execute the body’s steps five times, assigning the iterator variable values from 0 to 4 on each iteration.
- To cycle through a collection of elements matching a specific selector, choose a selector as the iterator. The loop will then process the steps in the body for each element found by the selector. The iterator variable will be assigned the selector for each element during each iteration. In the example below, the
clickstep uses$reviewto click each element in turn.
Note: By default, the loop’s iterator variable is named item, but it can be renamed as needed.
Steps and their descriptions
- CLICK (
click): Simulates a mouse click on a specified element, such as a button or a link, triggering any associated actions or events. - DRAG (
drag): Replicates a drag-and-drop action by clicking, holding, and moving an element from one location to another within the browser. - FILL (
fill): Fills a form field, such as a text box, with the specified value. - FOCUS (
focus): Sets the focus on a specified element, like an input field or a dropdown menu, making it the active target for user interactions. - GO_BACK (
go_back): Goes back to the previous page in the browser history, like the browser’s back button. - KEYPRESS (
keypress): Simulates a keyboard key press event on a focused element, such as typing a letter or pressing Enter. - MOUSEMOVE (
mousemove): Mimics the movement of the mouse pointer over a specified element, triggering any related hover effects or events. - OPEN (
open): Opens a specified URL in the browser, navigating to the desired web page. - SCROLL (
scroll): Scrolls the browser window to a specified position or element, bringing the target into view. - SELECT (
select): Selects an option within a dropdown menu or similar element, making it the active choice. - TYPE (
type): Simulates typing text into a focused input field, such as a text box or search bar. - WAIT_DOC (
wait_doc): Pauses the macro execution until the web page’s content is fully loaded. - WAIT_FOR_DURATION (
wait_for_duration): Introduces a pause in the macro execution for a specified number of seconds. - WAIT_FOR_ELEMENT (
wait_for_element): Waits for a specified element to become available on the web page before proceeding with the macro execution.
Troubleshooting & how to check if a Macro is working correctly
When you replay a Macro, the sequence of steps will execute and successfully executed steps will have a green check mark. If a Macro is working correctly you will see a green check mark against all the recorded steps.
Macros error out usually when:
- The page didn’t load correctly.
- The page loads correctly but the page is a different version of the page that cannot be handled with current recorded steps.
- The page loaded correctly but the elements’ identifiers used to build selectors have changed. The steps fail by not being able to locate the elements correctly.
If you encounter an error, you will see that the Macro does not proceed beyond the step that has an issue. If a monitor is associated to a Macro with an issue, its check fails with the failed step’s own error code, such as E_ELEMENT_NOT_FOUND or E_ASSERT. The check fails with E_MACRO when the error has no code and is not a timeout, network or invalid selector error, or when the Macro runs longer than 30 minutes. To learn more about the issue, click the time in the monitor’s row in the Watchlist to open its Check log, then click the error entry. The check details open in a new tab with the error details. When the check saved the Macro’s steps, the page also lists them and marks the step that failed.
The check details show the error in Error Details and mark the failed step, and the blocks that hold it, with a red cross. You can go back to replay the Macro and find out the step with issues and either edit or re-record the step.
Need help?
If you need help with a macro or have a URL that is not handled yet, feel free to ask in the Distill forum. You can explore existing discussions or post to get help on any topic.
For instructions on how to import, export, and edit macros on a webpage using JSON refer this video.
Suggested reading
Below are some related resources for more details on how to use conditions within macros:
Video guide to check login status of page using conditions in Macros.
Video tutorial to use conditional IF block to handle infrequent Pop-ups while adding Macros steps.
Discussion thread on Conditions in Macros for further reading.