Skip to content

Usage

All examples are in Kotlin. Java is also supported. Add the Maven dependencies from the Install page first.


Workflow Automation (fine-grained browser control)

Open a page, drive Chrome with natural-language instructions via the companion AI agent, read the live DOM, and extract structured fields:

kotlin
val session = AgenticContexts.getOrCreateSession()
val agent = session.companionAgent
val driver = session.getOrCreateBoundDriver()

// Open the target URL in a managed Chrome session
var page = session.open(url)

// Drive the browser with a natural-language instruction
agent.act("scroll to the comment section")

// Read a DOM node directly from the live page
val content = driver.selectFirstTextOrNull("#comments")

// Snapshot the page into an in-memory document for offline parsing
var document = session.parse(page)

// Extract structured fields using CSS selectors in one call
var fields = session.extract(document, mapOf("title" to "#title"))

// Run a multi-step agentic flow (navigate, search, open a product)
val history = agent.run(
    "Go to amazon.com, search for 'smart phone', open the product page with the highest ratings"
)

// Capture the updated browser state back into a snapshot
page = session.capture(driver)
document = session.parse(page)
fields = session.extract(document, mapOf("ratings" to "#ratings"))

X-SQL Extraction (LLM + CSS selectors in SQL syntax)

X-SQL lets you combine llm_extract() with standard DOM selector functions. This example extracts several fields from an Amazon product page in a single query:

kotlin
val context = AgenticContexts.create()

val sql = """
select
  llm_extract(dom, 'product name, price, ratings') as llm_extracted_data,
  dom_first_text(dom, '#productTitle') as title,
  dom_first_text(dom, '#bylineInfo') as brand,
  dom_first_text(dom, '#price tr td:matches(^Price) ~ td') as price,
  dom_first_text(dom, '#acrCustomerReviewText') as ratings,
  str_first_float(dom_first_text(dom, '#reviewsMedley .AverageCustomerReviews span:contains(out of)'), 0.0) as score
from load_and_select('https://www.amazon.com/dp/B08PP5MSVB -i 1s -njr 3', 'body');
"""

val rs = context.executeQuery(sql)
println(ResultSetFormatter(rs, withHeader = true))

The load_and_select function fetches the URL with a 1-second cache (-i 1s) and retries up to 3 times (-njr 3).


High-Speed Parallel Processing

Submit hundreds of URLs concurrently with resource blocking to maximize throughput:

kotlin
val args = "-refresh -dropContent -interactLevel fastest"
val blockingUrls = listOf("*.png", "*.jpg")

val links = LinkExtractors.fromResource("urls.txt")
    .map { ListenableHyperlink(it, "", args = args) }
    .onEach {
        it.eventHandlers.browseEventHandlers.onWillNavigate.addLast { page, driver ->
            driver.addBlockedURLs(blockingUrls)
        }
    }

session.submitAll(links)

Images and other binary assets are blocked before the page loads, cutting bandwidth and latency.


Example Repositories