Actions

Complete reference for browser actions — navigate, click, type, screenshot, extract data, scroll, and wait.

Overview

Browser actions are the building blocks for web interaction. Each action operates on an active browser session and returns a typed result.

Navigate the browser to a URL.

await tx.browsers.navigate(session.id, {
  url: 'https://example.com',
  waitUntil: 'networkidle', // 'load' | 'domcontentloaded' | 'networkidle'
});
ParameterTypeDefaultDescription
urlstringrequiredThe URL to navigate to
waitUntilstring'load'When to consider navigation complete

Interaction

click

Click an element on the page.

await tx.browsers.click(session.id, {
  selector: 'button.submit',
  button: 'left', // 'left' | 'right' | 'middle'
  clickCount: 1,
});
ParameterTypeDefaultDescription
selectorstringrequiredCSS selector for the target element
buttonstring'left'Mouse button to click
clickCountnumber1Number of clicks (2 for double-click)

type

Type text into an input or textarea.

await tx.browsers.type(session.id, {
  selector: 'input[name="email"]',
  text: 'user@example.com',
  delay: 50, // milliseconds between keystrokes
});
ParameterTypeDefaultDescription
selectorstringrequiredCSS selector for the input element
textstringrequiredText to type
delaynumber0Delay between keystrokes in ms (simulates human typing)

scroll

Scroll the page or a specific element.

// Scroll the page down
await tx.browsers.scroll(session.id, {
  direction: 'down',
  amount: 500, // pixels
});
 
// Scroll to a specific element
await tx.browsers.scroll(session.id, {
  selector: '#footer',
  behavior: 'smooth',
});
ParameterTypeDefaultDescription
directionstring'up', 'down', 'left', 'right'
amountnumber300Pixels to scroll
selectorstringScroll until this element is visible
behaviorstring'auto''auto' or 'smooth'

Data Extraction

extractText

Extract text content from elements matching a selector.

const result = await tx.browsers.extractText(session.id, {
  selector: '.article-body p',
});
 
console.log(result.text); // Combined text of all matching elements
ParameterTypeDefaultDescription
selectorstringrequiredCSS selector for target elements

Returns: { text: string }

extractStructured

Extract structured data from repeating elements into typed objects.

const result = await tx.browsers.extractStructured(session.id, {
  selector: '.product-card',
  schema: {
    name: { selector: 'h3', attribute: 'textContent' },
    price: { selector: '.price', attribute: 'textContent' },
    image: { selector: 'img', attribute: 'src' },
    link: { selector: 'a', attribute: 'href' },
  },
});
 
// result.items is an array of objects matching the schema
for (const product of result.items) {
  console.log(product.name, product.price);
}
ParameterTypeDefaultDescription
selectorstringrequiredCSS selector for the repeating container
schemaobjectrequiredField definitions with inner selectors and attributes

Schema field options:

  • selector — CSS selector relative to the container element
  • attribute — DOM attribute to extract (textContent, href, src, value, etc.)

Returns: { items: T[] }

Visual Capture

screenshot

Capture a screenshot of the current page.

const result = await tx.browsers.screenshot(session.id, {
  fullPage: true,
  format: 'png',
  quality: 90,
  selector: undefined, // or a CSS selector for element-level capture
});
 
// result.data is base64-encoded image data
const buffer = Buffer.from(result.data, 'base64');
ParameterTypeDefaultDescription
fullPagebooleanfalseCapture entire scrollable page
formatstring'png''png' or 'jpeg'
qualitynumber80JPEG quality (1-100), ignored for PNG
selectorstringCapture a specific element only

Returns: { data: string, width: number, height: number }

Synchronization

waitFor

Wait for an element to appear, disappear, or reach a specific state.

// Wait for element to appear
await tx.browsers.waitFor(session.id, {
  selector: '.results-loaded',
  state: 'visible',
  timeout: 10000,
});
 
// Wait for element to disappear (e.g., loading spinner)
await tx.browsers.waitFor(session.id, {
  selector: '.loading-spinner',
  state: 'hidden',
  timeout: 15000,
});
ParameterTypeDefaultDescription
selectorstringrequiredCSS selector to watch
statestring'visible''visible', 'hidden', 'attached', 'detached'
timeoutnumber30000Maximum wait time in ms

Error Handling

All actions throw typed errors when they fail:

import { BrowserError, BrowserTimeoutError } from '@usetransactional/node';
 
try {
  await tx.browsers.waitFor(session.id, {
    selector: '.nonexistent',
    timeout: 5000,
  });
} catch (error) {
  if (error instanceof BrowserTimeoutError) {
    console.log('Element did not appear within timeout');
  } else if (error instanceof BrowserError) {
    console.log('Browser action failed:', error.message);
  }
}

Action Chaining Pattern

Actions are designed to be composed sequentially for multi-step workflows:

// Login flow
await tx.browsers.navigate(session.id, { url: 'https://app.example.com/login' });
await tx.browsers.type(session.id, { selector: '#email', text: 'user@example.com' });
await tx.browsers.type(session.id, { selector: '#password', text: 'secret' });
await tx.browsers.click(session.id, { selector: 'button[type="submit"]' });
await tx.browsers.waitFor(session.id, { selector: '.dashboard', timeout: 10000 });
 
const data = await tx.browsers.extractText(session.id, {
  selector: '.welcome-message',
});

Next Steps