Sample macro scripts#
A few sample macros for common situations. Adjust the selectors, timings and repeat counts to fit your page before you use them.
Wait for the page to load, then click a random internal link#
Wait for the page to finish loading, pause for a moment like a person reading, then click a random link on the same domain.
await WaitForLoading();
await Delay(1000, 3000);
await ClickRandomInternalLink();
await WaitForLoading();
Type a keyword into the search box, then press Enter#
Click the search box, type the keyword at a random speed, then press Enter to submit the form.
await WaitForLoading();
await ClickBySelector('input[type="search"]');
await Typing("9hits traffic exchange", 100, 250);
await SendKeyPress(K_ENTER);
await WaitForLoading();
Scroll down slowly, with random pauses in between#
Scroll in several short steps, each by a random distance, with a random pause between two steps, instead of jumping straight to the bottom of the page.
await WaitForLoading();
let y = 0;
for (let i = 0; i < 5; i++) {
y += Random(400, 800); // scroll a different distance each time
await ScrollTo(0, y, true);
await Delay(1500, 4000); // random 1.5-4 second pause between scrolls
}
Wait for an element to appear, then click it#
When the button you want appears late (for example a "Load more" button), clicking right away can miss. Wait for the button to appear, then click it.
await WaitForLoading();
const appeared = await WaitForElmtBySelector("#load-more", { exists: true }, 20);
if (appeared) {
await ClickBySelector("#load-more");
}
Read the page title with EvalScript and log it#
EvalScript runs code inside the page, so it can read document.title. The result is always
wrapped in an object; get the value through .result. Log prints the value to the Logs panel in Studio.
await WaitForLoading();
const title = (await EvalScript("document.title"))?.result;
Log("Page title:", title);
Search, then read the data the page loads#
Many pages do not reload when you click search: they fetch data in the background and then draw the
results. Instead of waiting for an element to appear, wait for that exact response with
WaitForResponse and read the data directly. Start waiting
before you click, and await only after the click.
await WaitForLoading();
await ClickBySelector('input[type="search"]');
await Typing("9hits", 100, 250);
const p = WaitForResponse("/api/search", 30, true);
await SendKeyPress(K_ENTER);
const res = await p;
if (res && res.status === 200) {
const data = JSON.parse(res.body);
Log("Number of results:", data.items.length);
} else {
Log("No search results received");
}
Open a link in a new tab, then close it when done#
Open a page in a separate tab to read information without leaving the page you are on, then close
that tab when you are done. After CloseTab, commands run on the
tab currently shown in the window, usually the original tab. These two tab functions do not run in
9Hits campaigns.