Documentation
¶
Overview ¶
Package gosurfer provides AI-powered browser automation in pure Go.
It wraps headless Chrome via the Chrome DevTools Protocol (CDP) and provides an intelligent agent that can autonomously browse the web, similar to Python's Browser Use library but optimized for Go.
Key features:
- Pure Go, no Node.js or Python dependency
- LLM-driven autonomous browsing (OpenAI, Anthropic, Ollama)
- Smart DOM extraction and serialization for LLM consumption
- Auto-waiting, network interception, screenshot/PDF
- Fits in a <100MB Docker container
Index ¶
- Variables
- func ApplyStealth(p *Page) error
- func GenerateTOTP(secret string) (string, error)
- func GenerateTOTPAt(secret string, t time.Time) (string, error)
- func HumanDelay(base, jitter time.Duration)
- type ActionContext
- type ActionDef
- type ActionRegistry
- type Agent
- type AgentConfig
- type AgentResult
- type AnthropicProvider
- type BoundingBox
- type Browser
- func (b *Browser) Close() error
- func (b *Browser) GrantPermissions(origin string, permissions ...string) error
- func (b *Browser) HandleAuth(username, password string) func() error
- func (b *Browser) Incognito() (*Browser, error)
- func (b *Browser) NewPage() (*Page, error)
- func (b *Browser) PageByURL(urlRegex string) (*Page, error)
- func (b *Browser) Pages() ([]*Page, error)
- func (b *Browser) ResetPermissions() error
- func (b *Browser) Rod() *rod.Browser
- func (b *Browser) WaitDownload() func() []byte
- type BrowserConfig
- type CAPTCHAInfo
- type CAPTCHASolver
- type CAPTCHAType
- type CapSolver
- type ChallengeType
- type ChatMessage
- type ChatOption
- type ChatResponse
- type ColorScheme
- type ContentPart
- type Cookie
- type DOMElement
- type DOMService
- type DOMState
- type Device
- type Dialog
- type Element
- func (e *Element) Attribute(name string) (string, error)
- func (e *Element) BBox() (*BoundingBox, error)
- func (e *Element) Clear() error
- func (e *Element) ClearAndType(text string) error
- func (e *Element) Click() error
- func (e *Element) DoubleClick() error
- func (e *Element) DragTo(target *Element) error
- func (e *Element) DragToCoordinates(x, y float64) error
- func (e *Element) Focus() error
- func (e *Element) Frame() (*Page, error)
- func (e *Element) HTML() (string, error)
- func (e *Element) Hover() error
- func (e *Element) HumanClick() error
- func (e *Element) HumanType(text string) error
- func (e *Element) Rod() *rod.Element
- func (e *Element) Screenshot() ([]byte, error)
- func (e *Element) ScrollIntoView() error
- func (e *Element) SelectOption(texts ...string) error
- func (e *Element) SelectOptionByValue(values ...string) error
- func (e *Element) ShadowRoot() (*Element, error)
- func (e *Element) Text() (string, error)
- func (e *Element) Type(text string) error
- func (e *Element) UploadFile(paths ...string) error
- func (e *Element) Visible() (bool, error)
- func (e *Element) WaitStable() error
- func (e *Element) WaitVisible() error
- type ExpectOption
- type HARRecorder
- type InterceptedRequest
- func (r *InterceptedRequest) Abort()
- func (r *InterceptedRequest) Body() string
- func (r *InterceptedRequest) Continue()
- func (r *InterceptedRequest) Header(key string) string
- func (r *InterceptedRequest) LoadResponse() error
- func (r *InterceptedRequest) Method() string
- func (r *InterceptedRequest) Respond(status int, body string, headerPairs ...string)
- func (r *InterceptedRequest) RespondJSON(status int, data interface{})
- func (r *InterceptedRequest) ResponseBody() string
- func (r *InterceptedRequest) ResponseStatus() int
- func (r *InterceptedRequest) SetResponseBody(body string)
- func (r *InterceptedRequest) SetResponseHeader(pairs ...string)
- func (r *InterceptedRequest) URL() string
- type LLMProvider
- type LocatorExpect
- func (le *LocatorExpect) Not() *LocatorExpect
- func (le *LocatorExpect) ToBeChecked() error
- func (le *LocatorExpect) ToBeDisabled() error
- func (le *LocatorExpect) ToBeEnabled() error
- func (le *LocatorExpect) ToBeHidden() error
- func (le *LocatorExpect) ToBeVisible() error
- func (le *LocatorExpect) ToContainText(substring string) error
- func (le *LocatorExpect) ToHaveAttribute(name, value string) error
- func (le *LocatorExpect) ToHaveCount(expected int) error
- func (le *LocatorExpect) ToHaveText(expected string) error
- func (le *LocatorExpect) ToHaveValue(expected string) error
- type LocatorOption
- type ManualCAPTCHASolver
- type NetworkInterceptor
- func (ni *NetworkInterceptor) BlockPatterns(patterns ...string) *NetworkInterceptor
- func (ni *NetworkInterceptor) MockJSON(pattern string, status int, data interface{}) *NetworkInterceptor
- func (ni *NetworkInterceptor) MockText(pattern string, status int, body string, headerPairs ...string) *NetworkInterceptor
- func (ni *NetworkInterceptor) OnRequest(pattern string, handler func(req *InterceptedRequest)) *NetworkInterceptor
- func (ni *NetworkInterceptor) Start()
- func (ni *NetworkInterceptor) Stop() error
- type NetworkRoute
- type OpenAIProvider
- type Page
- func (p *Page) AutoDismissDialogs() func()
- func (p *Page) Back() error
- func (p *Page) ClearCookies() error
- func (p *Page) ClearGeolocation() error
- func (p *Page) Click(selector string) error
- func (p *Page) Close() error
- func (p *Page) DOMState() (*DOMState, error)
- func (p *Page) DOMStateWithScreenshot() (*DOMState, error)
- func (p *Page) DeleteCookies(name string) error
- func (p *Page) DetectCAPTCHA() (*CAPTCHAInfo, error)
- func (p *Page) DetectChallenge() (ChallengeType, error)
- func (p *Page) DragDrop(fromX, fromY, toX, toY float64) error
- func (p *Page) Element(selector string) (*Element, error)
- func (p *Page) ElementByXPath(xpath string) (*Element, error)
- func (p *Page) Elements(selector string) ([]*Element, error)
- func (p *Page) EmulateDevice(d Device) error
- func (p *Page) Eval(js string, args ...interface{}) (interface{}, error)
- func (p *Page) FocusedDOMState() (*DOMState, error)
- func (p *Page) Forward() error
- func (p *Page) Frames() ([]*Page, error)
- func (p *Page) FullScreenshot() ([]byte, error)
- func (p *Page) GetAllByRole(role string, opts ...LocatorOption) ([]*Element, error)
- func (p *Page) GetAllByText(text string, opts ...LocatorOption) ([]*Element, error)
- func (p *Page) GetByAltText(text string, opts ...LocatorOption) (*Element, error)
- func (p *Page) GetByLabel(text string, opts ...LocatorOption) (*Element, error)
- func (p *Page) GetByPlaceholder(text string, opts ...LocatorOption) (*Element, error)
- func (p *Page) GetByRole(role string, opts ...LocatorOption) (*Element, error)
- func (p *Page) GetByTestID(id string) (*Element, error)
- func (p *Page) GetByText(text string, opts ...LocatorOption) (*Element, error)
- func (p *Page) GetCookie(name string) (string, error)
- func (p *Page) GetCookies() ([]Cookie, error)
- func (p *Page) GetStorageState() (*StorageState, error)
- func (p *Page) HTML() (string, error)
- func (p *Page) HandleDialog() (wait func() *Dialog, handle func(accept bool, promptText string) error)
- func (p *Page) HandleFileDialog() (func(paths []string) error, error)
- func (p *Page) HumanClick(selector string) error
- func (p *Page) HumanMoveMouse(toX, toY float64) error
- func (p *Page) HumanScroll(deltaY float64) error
- func (p *Page) HumanType(selector string, text string) error
- func (p *Page) Intercept() *NetworkInterceptor
- func (p *Page) IsIframe() bool
- func (p *Page) KeyPress(key input.Key) error
- func (p *Page) LoadStorageState(path string) error
- func (p *Page) LocalStorageAll() (map[string]string, error)
- func (p *Page) LocalStorageClear() error
- func (p *Page) LocalStorageDelete(key string) error
- func (p *Page) LocalStorageGet(key string) (string, error)
- func (p *Page) LocalStorageSet(key, value string) error
- func (p *Page) Navigate(url string) error
- func (p *Page) PDF() ([]byte, error)
- func (p *Page) Reload() error
- func (p *Page) RestoreStorageState(state *StorageState) error
- func (p *Page) Rod() *rod.Page
- func (p *Page) SaveStorageState(path string) error
- func (p *Page) Screenshot() ([]byte, error)
- func (p *Page) ScreenshotJPEG(quality int) ([]byte, error)
- func (p *Page) Scroll(dx, dy float64) error
- func (p *Page) ScrollToBottom() error
- func (p *Page) ScrollToTop() error
- func (p *Page) SessionStorageGet(key string) (string, error)
- func (p *Page) SessionStorageSet(key, value string) error
- func (p *Page) SetColorScheme(scheme ColorScheme) error
- func (p *Page) SetCookie(name, value, domain, path string) error
- func (p *Page) SetCookies(cookies []Cookie) error
- func (p *Page) SetGeolocation(latitude, longitude, accuracy float64) error
- func (p *Page) SetLocale(locale string) error
- func (p *Page) SetNetworkConditions(latencyMs float64, downloadBytesPerSec, uploadBytesPerSec float64) error
- func (p *Page) SetOffline(offline bool) error
- func (p *Page) SetReducedMotion(motion ReducedMotion) error
- func (p *Page) SetTimezone(timezoneID string) error
- func (p *Page) SetTouchEnabled(enabled bool) error
- func (p *Page) SetUserAgent(userAgent string) error
- func (p *Page) SetViewport(width, height int, scaleFactor float64, mobile bool) error
- func (p *Page) SolveCAPTCHA(ctx context.Context, solver CAPTCHASolver) error
- func (p *Page) StartHAR() *HARRecorder
- func (p *Page) TargetID() string
- func (p *Page) Text(selector string) (string, error)
- func (p *Page) Title() (string, error)
- func (p *Page) Type(selector, text string) error
- func (p *Page) URL() string
- func (p *Page) WaitForChallenge(timeout time.Duration) (ChallengeType, time.Duration, error)
- func (p *Page) WaitIdle(timeout time.Duration) error
- func (p *Page) WaitLoad() error
- func (p *Page) WaitPopup() func() (*Page, error)
- func (p *Page) WaitSelector(selector string) (*Element, error)
- func (p *Page) WaitStable(interval time.Duration) error
- type PageExpect
- func (e *PageExpect) Locator(selector string) *LocatorExpect
- func (e *PageExpect) ToHaveTitle(expected string) error
- func (e *PageExpect) ToHaveTitleContaining(substring string) error
- func (e *PageExpect) ToHaveURL(expected string) error
- func (e *PageExpect) ToHaveURLContaining(substring string) error
- type ParamDef
- type ReducedMotion
- type Secrets
- type StepInfo
- type StorageState
- type TabInfo
- type TokenUsage
- type TwoCaptchaSolver
Constants ¶
This section is empty.
Variables ¶
var ( DeviceIPhoneX = Device{ Name: "iPhone X", UserAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1", Width: 375, Height: 812, Scale: 3.0, Mobile: true, Touch: true, } DeviceIPhone14Pro = Device{ Name: "iPhone 14 Pro", UserAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1", Width: 393, Height: 852, Scale: 3.0, Mobile: true, Touch: true, } DevicePixel7 = Device{ Name: "Pixel 7", UserAgent: "Mozilla/5.0 (Linux; Android 14; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36", Width: 412, Height: 915, Scale: 2.625, Mobile: true, Touch: true, } DeviceIPadPro = Device{ Name: "iPad Pro 12.9", UserAgent: "Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1", Width: 1024, Height: 1366, Scale: 2.0, Mobile: true, Touch: true, } DeviceGalaxyS23 = Device{ Name: "Galaxy S23", UserAgent: "Mozilla/5.0 (Linux; Android 14; SM-S911B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36", Width: 360, Height: 780, Scale: 3.0, Mobile: true, Touch: true, } DeviceDesktop1080p = Device{ Name: "Desktop 1080p", UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", Width: 1920, Height: 1080, Scale: 1.0, Mobile: false, Touch: false, } DeviceDesktop4K = Device{ Name: "Desktop 4K", UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", Width: 3840, Height: 2160, Scale: 2.0, Mobile: false, Touch: false, } )
Pre-configured device profiles.
Functions ¶
func ApplyStealth ¶
ApplyStealth injects anti-detection scripts into a page. Must be called before navigating to a URL. The scripts run before any page JavaScript, patching common bot-detection vectors.
func GenerateTOTP ¶
GenerateTOTP generates a 6-digit TOTP code from a base32-encoded secret. Implements RFC 6238 with a 30-second time step and HMAC-SHA1.
func GenerateTOTPAt ¶
GenerateTOTPAt generates a TOTP code for a specific time.
func HumanDelay ¶ added in v0.2.8
HumanDelay pauses for a random duration that mimics human reaction time. Base is the minimum delay; jitter adds randomness up to that additional amount.
gosurfer.HumanDelay(200*time.Millisecond, 300*time.Millisecond) // 200-500ms
Types ¶
type ActionContext ¶
ActionContext provides the browser state to action handlers.
type ActionDef ¶
type ActionDef struct {
Name string `json:"name"`
Description string `json:"description"`
Params []ParamDef `json:"params"`
Run func(ctx context.Context, ac ActionContext, params map[string]interface{}) (string, error)
}
ActionDef defines a browser action that the agent can execute.
type ActionRegistry ¶
type ActionRegistry struct {
// contains filtered or unexported fields
}
ActionRegistry manages available actions.
func DefaultActions ¶
func DefaultActions() *ActionRegistry
DefaultActions returns the built-in action set.
func NewActionRegistry ¶
func NewActionRegistry() *ActionRegistry
NewActionRegistry creates an empty action registry.
func (*ActionRegistry) Actions ¶
func (r *ActionRegistry) Actions() []*ActionDef
Actions returns all registered actions in order.
func (*ActionRegistry) Get ¶
func (r *ActionRegistry) Get(name string) (*ActionDef, bool)
Get returns an action by name.
func (*ActionRegistry) Register ¶
func (r *ActionRegistry) Register(action *ActionDef)
Register adds an action to the registry.
type Agent ¶
type Agent struct {
// contains filtered or unexported fields
}
Agent is an LLM-driven autonomous browser that completes tasks.
func NewAgent ¶
func NewAgent(config AgentConfig) (*Agent, error)
NewAgent creates a new browsing agent.
type AgentConfig ¶
type AgentConfig struct {
// Task is the natural language description of what to accomplish.
Task string
// LLM is the language model provider for decision-making.
LLM LLMProvider
// Browser is an existing browser instance. If nil, a new headless one is created.
Browser *Browser
// MaxSteps is the maximum number of agent steps (default 50).
MaxSteps int
// MaxFailures is the max consecutive failures before stopping (default 5).
MaxFailures int
// UseVision includes screenshots in LLM context when true.
UseVision bool
// Headless controls browser visibility if a new browser is created.
Headless bool
// OnStep is called after each step with progress info.
OnStep func(StepInfo)
// Verbose enables detailed logging.
Verbose bool
// MaxTokens sets the max tokens per LLM call (default 4096).
MaxTokens int
// Temperature sets the LLM sampling temperature (default 0.0).
Temperature float64
// CAPTCHASolver is an optional CAPTCHA solving backend.
// When set, the agent automatically detects and solves CAPTCHAs.
CAPTCHASolver CAPTCHASolver
// Secrets stores sensitive data (credentials, TOTP secrets).
// Keys ending in "_totp" auto-generate TOTP codes on access.
// Use {{key_name}} placeholders in typed text for auto-replacement.
Secrets map[string]string
// Stealth enables anti-detection mode on the browser.
Stealth bool
// DisableSummary disables LLM-powered context summarization for long tasks.
// When false (default), the agent summarizes older steps to maintain awareness
// of earlier context beyond the 5-step recent history window.
DisableSummary bool
// SummaryLLM is an optional cheaper/faster LLM used for context summarization.
// If nil, the main LLM is used. Useful for pairing an expensive reasoning model
// (e.g. Opus, GPT-4) with a cheaper summarizer (e.g. Haiku, GPT-4.1-mini).
SummaryLLM LLMProvider
// FocusContent strips boilerplate (nav, footer, cookie banners, social links,
// terms/privacy links) from DOM extraction, reducing token usage by 30-60%.
// Content is focused on <main>, <article>, [role="main"] regions.
FocusContent bool
}
AgentConfig configures the AI browsing agent.
type AgentResult ¶
type AgentResult struct {
// Success indicates whether the task was completed.
Success bool
// Output is the final answer or result text.
Output string
// Steps is the total number of steps taken.
Steps int
// History contains details of each step.
History []StepInfo
// TotalTokens is the cumulative token usage.
TotalTokens TokenUsage
}
AgentResult is the final result of an agent run.
type AnthropicProvider ¶
type AnthropicProvider struct {
// contains filtered or unexported fields
}
AnthropicProvider implements LLMProvider for the Anthropic Messages API.
func NewAnthropic ¶
func NewAnthropic(apiKey, model string) *AnthropicProvider
NewAnthropic creates an Anthropic provider.
func (*AnthropicProvider) ChatCompletion ¶
func (p *AnthropicProvider) ChatCompletion(ctx context.Context, messages []ChatMessage, opts ...ChatOption) (*ChatResponse, error)
func (*AnthropicProvider) Name ¶
func (p *AnthropicProvider) Name() string
type BoundingBox ¶
type BoundingBox struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Width float64 `json:"width"`
Height float64 `json:"height"`
}
BoundingBox represents an element's position and size.
type Browser ¶
type Browser struct {
// contains filtered or unexported fields
}
Browser wraps a Chrome/Chromium instance.
func ConnectBrowser ¶
func ConnectBrowser(wsURL string, cfg ...BrowserConfig) (*Browser, error)
ConnectBrowser connects to an existing Chrome instance via a CDP WebSocket URL.
func NewBrowser ¶
func NewBrowser(cfg ...BrowserConfig) (*Browser, error)
NewBrowser launches a new Chrome/Chromium instance.
func (*Browser) GrantPermissions ¶ added in v0.2.0
GrantPermissions grants browser permissions for the given origin. Common permissions: "geolocation", "notifications", "camera", "microphone", "clipboard-read", "clipboard-write".
func (*Browser) HandleAuth ¶
HandleAuth sets up HTTP Basic authentication handling.
func (*Browser) Incognito ¶
Incognito creates an isolated browser context with separate cookies/storage.
func (*Browser) ResetPermissions ¶ added in v0.2.0
ResetPermissions resets all permission overrides.
func (*Browser) WaitDownload ¶
WaitDownload sets up a download handler that returns file bytes when the next download completes. Call before triggering the download.
type BrowserConfig ¶
type BrowserConfig struct {
// Headless runs the browser without a visible window.
Headless bool
// ExecPath is the path to Chrome/Chromium. If empty, rod auto-detects.
ExecPath string
// UserDataDir persists browser state (cookies, localStorage, etc.).
UserDataDir string
// Proxy sets an HTTP/SOCKS proxy (e.g., "socks5://127.0.0.1:1080").
Proxy string
// WindowWidth sets the browser window width. Default: 1280.
WindowWidth int
// WindowHeight sets the browser window height. Default: 720.
WindowHeight int
// NoSandbox disables the Chromium sandbox (required in Docker).
NoSandbox bool
// Stealth enables anti-detection mode (patches navigator.webdriver,
// spoofs plugins/WebGL, sets realistic user agent, etc.).
Stealth bool
// HumanMode enables maximum anti-detection: system Chrome, new headless mode,
// stealth patches, and human-like behavior (random delays, mouse movement).
// Automatically sets Stealth=true and uses --headless=new.
HumanMode bool
// AllowedDomains restricts navigation to these domains (glob patterns).
AllowedDomains []string
// BlockedDomains prevents navigation to these domains (glob patterns).
BlockedDomains []string
// ChallengeWaitTimeout controls how long Navigate() waits for an
// auto-solvable bot-protection challenge (e.g., Cloudflare's "Just a
// moment..." JS challenge) to clear. If 0, the default of 15s is used.
// Set to -1 to disable auto-waiting entirely.
ChallengeWaitTimeout time.Duration
}
BrowserConfig configures browser launch options.
type CAPTCHAInfo ¶
type CAPTCHAInfo struct {
Type CAPTCHAType
SiteKey string
PageURL string
}
CAPTCHAInfo describes a detected CAPTCHA on a page.
type CAPTCHASolver ¶
type CAPTCHASolver interface {
// Solve sends the CAPTCHA to a solving service and returns the token.
Solve(ctx context.Context, info CAPTCHAInfo) (string, error)
// Name returns the solver name for logging.
Name() string
}
CAPTCHASolver is the interface for CAPTCHA solving backends.
type CAPTCHAType ¶
type CAPTCHAType string
CAPTCHAType identifies the CAPTCHA provider.
const ( CAPTCHAReCaptchaV2 CAPTCHAType = "recaptcha_v2" CAPTCHAReCaptchaV3 CAPTCHAType = "recaptcha_v3" CAPTCHAHCaptcha CAPTCHAType = "hcaptcha" CAPTCHATurnstile CAPTCHAType = "turnstile" )
type CapSolver ¶
type CapSolver struct {
APIKey string
BaseURL string // default: https://api.capsolver.com
// contains filtered or unexported fields
}
CapSolver implements CAPTCHASolver using the capsolver.com API.
func NewCapSolver ¶
NewCapSolver creates a CapSolver solver.
type ChallengeType ¶ added in v0.5.0
type ChallengeType string
ChallengeType identifies a specific bot-protection challenge.
const ( // ChallengeNone means the page is not currently showing a challenge. ChallengeNone ChallengeType = "" // ChallengeCloudflareUAM is Cloudflare "Under Attack Mode" — the classic // "Just a moment..." JavaScript challenge that resolves in ~5-15 seconds. ChallengeCloudflareUAM ChallengeType = "cloudflare_uam" // ChallengeCloudflareTurnstile is Cloudflare's newer interactive challenge. // These usually require user interaction and cannot be auto-solved. ChallengeCloudflareTurnstile ChallengeType = "cloudflare_turnstile" // ChallengeDataDome is captcha-delivery.com's fingerprint-based blocker. // Usually not auto-solvable; detection exists so callers can fail fast. ChallengeDataDome ChallengeType = "datadome" )
func (ChallengeType) IsAutoSolvable ¶ added in v0.5.0
func (c ChallengeType) IsAutoSolvable() bool
IsAutoSolvable reports whether a challenge type can be solved by waiting. Cloudflare UAM's JS challenge resolves itself; Turnstile and DataDome require user interaction or specialized bypass tooling.
type ChatMessage ¶
type ChatMessage struct {
Role string `json:"role"` // "system", "user", "assistant"
Content []ContentPart `json:"-"`
}
ChatMessage represents a message in the conversation.
func ImageMessage ¶
func ImageMessage(role, text string, imageData []byte, mimeType string) ChatMessage
ImageMessage creates a message with text and an image.
func TextMessage ¶
func TextMessage(role, text string) ChatMessage
TextMessage creates a simple text message.
type ChatOption ¶
type ChatOption func(*chatConfig)
ChatOption configures a chat completion request.
func WithJSONMode ¶
func WithJSONMode() ChatOption
WithJSONMode requests JSON output from the model.
func WithMaxTokens ¶
func WithMaxTokens(n int) ChatOption
WithMaxTokens sets the maximum response tokens.
func WithTemperature ¶
func WithTemperature(t float64) ChatOption
WithTemperature sets the sampling temperature.
type ChatResponse ¶
type ChatResponse struct {
Content string `json:"content"`
Usage TokenUsage `json:"usage"`
}
ChatResponse is the LLM's response.
type ColorScheme ¶ added in v0.2.0
type ColorScheme string
ColorScheme represents a CSS color scheme preference.
const ( ColorSchemeLight ColorScheme = "light" ColorSchemeDark ColorScheme = "dark" ColorSchemeNoPreference ColorScheme = "" )
type ContentPart ¶
type ContentPart struct {
Type string `json:"type"` // "text" or "image"
Text string `json:"text,omitempty"`
ImageB64 string `json:"image_b64,omitempty"`
MimeType string `json:"mime_type,omitempty"`
}
ContentPart is a piece of a message (text or image).
type Cookie ¶
type Cookie struct {
Name string `json:"name"`
Value string `json:"value"`
Domain string `json:"domain"`
Path string `json:"path"`
Expires float64 `json:"expires,omitempty"`
HTTPOnly bool `json:"httpOnly,omitempty"`
Secure bool `json:"secure,omitempty"`
SameSite string `json:"sameSite,omitempty"`
}
Cookie represents a browser cookie.
type DOMElement ¶
type DOMElement struct {
Index int `json:"index"`
Tag string `json:"tag"`
Text string `json:"text"`
Attributes map[string]string `json:"attributes"`
Rect BoundingBox `json:"rect"`
IsEditable bool `json:"is_editable"`
IsScrollable bool `json:"is_scrollable"`
Depth int `json:"depth"`
CSSSelector string `json:"css_selector"`
}
DOMElement represents an interactive element extracted from the page.
type DOMService ¶
type DOMService struct {
// contains filtered or unexported fields
}
DOMService handles DOM extraction and serialization.
func (*DOMService) GetFocusedState ¶ added in v0.2.3
func (d *DOMService) GetFocusedState() (*DOMState, error)
GetFocusedState extracts the DOM state with boilerplate stripped. Removes navigation, footers, cookie banners, ad containers, and low-value links (terms, privacy, copyright, same-page anchors, social media). Focuses on <main>, <article>, [role="main"] content regions.
func (*DOMService) GetState ¶
func (d *DOMService) GetState() (*DOMState, error)
GetState extracts the current DOM state, serialized for LLM consumption.
type DOMState ¶
type DOMState struct {
// URL is the current page URL.
URL string `json:"url"`
// Title is the page title.
Title string `json:"title"`
// Tree is the serialized DOM in indexed-element format for LLM consumption.
// Interactive elements are tagged with [index] prefixes.
Tree string `json:"tree"`
// Elements maps element indices to their metadata for action execution.
Elements map[int]*DOMElement `json:"elements"`
// Tabs lists all open browser tabs.
Tabs []TabInfo `json:"tabs,omitempty"`
// Screenshot is an optional JPEG screenshot of the current viewport.
Screenshot []byte `json:"-"`
// ScrollPosition indicates current scroll percentage (0-100).
ScrollPosition float64 `json:"scroll_position"`
// PageHeight is the total page height in pixels.
PageHeight float64 `json:"page_height"`
// ViewportHeight is the visible viewport height in pixels.
ViewportHeight float64 `json:"viewport_height"`
}
DOMState represents the current page state, optimized for LLM consumption.
type Device ¶ added in v0.2.0
type Device struct {
Name string
UserAgent string
Width int
Height int
Scale float64
Mobile bool
Touch bool
}
Device represents a pre-configured device profile for emulation.
type Dialog ¶
type Dialog struct {
Type string // "alert", "confirm", "prompt", "beforeunload"
Message string
DefaultPrompt string
// contains filtered or unexported fields
}
Dialog represents a JavaScript dialog (alert, confirm, prompt, beforeunload).
type Element ¶
type Element struct {
Index int // Index in DOM serialization (set by DOMState)
// contains filtered or unexported fields
}
Element wraps a DOM element with interaction methods.
func (*Element) BBox ¶
func (e *Element) BBox() (*BoundingBox, error)
BBox returns the element's bounding box in viewport coordinates.
func (*Element) ClearAndType ¶
ClearAndType clears the element then types new text.
func (*Element) Click ¶
Click clicks the element. It waits for the element to be visible and stable.
func (*Element) DoubleClick ¶
DoubleClick double-clicks the element.
func (*Element) DragToCoordinates ¶
DragToCoordinates drags this element to specific viewport coordinates.
func (*Element) Frame ¶
Frame returns a Page representing the content of this iframe element. Panics if the element is not an iframe.
func (*Element) HumanClick ¶ added in v0.2.8
HumanClick clicks the element with random offset from center.
func (*Element) HumanType ¶ added in v0.2.8
HumanType types text with random inter-keystroke delays.
func (*Element) Screenshot ¶
Screenshot captures a screenshot of just this element.
func (*Element) ScrollIntoView ¶
ScrollIntoView scrolls the element into the viewport.
func (*Element) SelectOption ¶
SelectOption selects a dropdown option by its visible text.
func (*Element) SelectOptionByValue ¶
SelectOptionByValue selects a dropdown option by its value attribute.
func (*Element) ShadowRoot ¶
ShadowRoot returns the shadow root of this element for querying shadow DOM.
func (*Element) Type ¶
Type types text into the element. It clears existing text first if clear is true.
func (*Element) UploadFile ¶
UploadFile sets files on a file input element.
func (*Element) WaitStable ¶
WaitStable waits until the element's position stops changing.
func (*Element) WaitVisible ¶
WaitVisible waits until the element becomes visible.
type ExpectOption ¶ added in v0.1.1
type ExpectOption func(*PageExpect)
ExpectOption configures assertion behavior.
func WithTimeout ¶ added in v0.1.1
func WithTimeout(d time.Duration) ExpectOption
WithTimeout sets the retry timeout for assertions.
type HARRecorder ¶
type HARRecorder struct {
// contains filtered or unexported fields
}
HARRecorder captures all network traffic on a page in HAR 1.2 format. Start recording before navigation to capture everything.
func (*HARRecorder) Entries ¶
func (rec *HARRecorder) Entries() int
Entries returns the number of recorded requests.
func (*HARRecorder) Export ¶
func (rec *HARRecorder) Export() ([]byte, error)
Export returns the recorded traffic as a HAR 1.2 JSON byte slice.
type InterceptedRequest ¶
type InterceptedRequest struct {
// contains filtered or unexported fields
}
InterceptedRequest wraps a hijacked network request.
func (*InterceptedRequest) Body ¶
func (r *InterceptedRequest) Body() string
Body returns the request body.
func (*InterceptedRequest) Continue ¶
func (r *InterceptedRequest) Continue()
Continue allows the request to proceed normally.
func (*InterceptedRequest) Header ¶
func (r *InterceptedRequest) Header(key string) string
Header returns a single request header value.
func (*InterceptedRequest) LoadResponse ¶ added in v0.2.0
func (r *InterceptedRequest) LoadResponse() error
LoadResponse fetches the real response from the server. Call this before reading or modifying the response.
func (*InterceptedRequest) Method ¶
func (r *InterceptedRequest) Method() string
Method returns the HTTP method.
func (*InterceptedRequest) Respond ¶
func (r *InterceptedRequest) Respond(status int, body string, headerPairs ...string)
Respond returns a custom response without hitting the real server.
req.Respond(200, "OK", "Content-Type", "text/plain")
func (*InterceptedRequest) RespondJSON ¶ added in v0.2.0
func (r *InterceptedRequest) RespondJSON(status int, data interface{})
RespondJSON returns a JSON response without hitting the real server. The data parameter is marshaled to JSON automatically.
req.RespondJSON(200, map[string]any{"status": "ok", "count": 42})
func (*InterceptedRequest) ResponseBody ¶ added in v0.2.0
func (r *InterceptedRequest) ResponseBody() string
ResponseBody returns the response body (after LoadResponse).
func (*InterceptedRequest) ResponseStatus ¶ added in v0.2.0
func (r *InterceptedRequest) ResponseStatus() int
ResponseStatus returns the response status code (after LoadResponse).
func (*InterceptedRequest) SetResponseBody ¶ added in v0.2.0
func (r *InterceptedRequest) SetResponseBody(body string)
SetResponseBody replaces the response body (after LoadResponse).
func (*InterceptedRequest) SetResponseHeader ¶ added in v0.2.0
func (r *InterceptedRequest) SetResponseHeader(pairs ...string)
SetResponseHeader sets a response header (after LoadResponse).
func (*InterceptedRequest) URL ¶
func (r *InterceptedRequest) URL() string
URL returns the request URL.
type LLMProvider ¶
type LLMProvider interface {
// ChatCompletion sends messages to the LLM and returns a response.
ChatCompletion(ctx context.Context, messages []ChatMessage, opts ...ChatOption) (*ChatResponse, error)
// Name returns the provider/model name.
Name() string
}
LLMProvider defines the interface for language model backends.
type LocatorExpect ¶ added in v0.1.1
type LocatorExpect struct {
// contains filtered or unexported fields
}
LocatorExpect provides auto-retrying element-level assertions.
func (*LocatorExpect) Not ¶ added in v0.1.1
func (le *LocatorExpect) Not() *LocatorExpect
Not returns a negated LocatorExpect where all assertions are inverted.
expect.Locator("#modal").Not().ToBeVisible() // asserts element is NOT visible
func (*LocatorExpect) ToBeChecked ¶ added in v0.1.1
func (le *LocatorExpect) ToBeChecked() error
ToBeChecked asserts the element (checkbox/radio) is checked.
func (*LocatorExpect) ToBeDisabled ¶ added in v0.1.1
func (le *LocatorExpect) ToBeDisabled() error
ToBeDisabled asserts the element has the disabled attribute.
func (*LocatorExpect) ToBeEnabled ¶ added in v0.1.1
func (le *LocatorExpect) ToBeEnabled() error
ToBeEnabled asserts the element is not disabled.
func (*LocatorExpect) ToBeHidden ¶ added in v0.1.1
func (le *LocatorExpect) ToBeHidden() error
ToBeHidden asserts the element is hidden or not in the DOM.
func (*LocatorExpect) ToBeVisible ¶ added in v0.1.1
func (le *LocatorExpect) ToBeVisible() error
ToBeVisible asserts the element is visible on the page.
func (*LocatorExpect) ToContainText ¶ added in v0.1.1
func (le *LocatorExpect) ToContainText(substring string) error
ToContainText asserts the element's text content contains the substring.
func (*LocatorExpect) ToHaveAttribute ¶ added in v0.1.1
func (le *LocatorExpect) ToHaveAttribute(name, value string) error
ToHaveAttribute asserts the element has an attribute with the given value.
func (*LocatorExpect) ToHaveCount ¶ added in v0.1.1
func (le *LocatorExpect) ToHaveCount(expected int) error
ToHaveCount asserts the number of elements matching the selector.
func (*LocatorExpect) ToHaveText ¶ added in v0.1.1
func (le *LocatorExpect) ToHaveText(expected string) error
ToHaveText asserts the element's text content equals the expected string.
func (*LocatorExpect) ToHaveValue ¶ added in v0.1.1
func (le *LocatorExpect) ToHaveValue(expected string) error
ToHaveValue asserts the element's value equals the expected string.
type LocatorOption ¶ added in v0.1.1
type LocatorOption func(*locatorConfig)
LocatorOption configures semantic locator behavior.
func Exact ¶ added in v0.1.1
func Exact() LocatorOption
Exact requires an exact text match instead of substring.
func Name ¶ added in v0.1.1
func Name(name string) LocatorOption
Name filters elements by their accessible name (for GetByRole).
type ManualCAPTCHASolver ¶
type ManualCAPTCHASolver struct {
SolveFunc func(ctx context.Context, info CAPTCHAInfo) (string, error)
}
ManualCAPTCHASolver calls a user-provided function to solve CAPTCHAs. Useful for custom solving services or human-in-the-loop flows.
func (*ManualCAPTCHASolver) Name ¶
func (s *ManualCAPTCHASolver) Name() string
func (*ManualCAPTCHASolver) Solve ¶
func (s *ManualCAPTCHASolver) Solve(ctx context.Context, info CAPTCHAInfo) (string, error)
type NetworkInterceptor ¶
type NetworkInterceptor struct {
// contains filtered or unexported fields
}
NetworkInterceptor manages request interception for a page.
func (*NetworkInterceptor) BlockPatterns ¶
func (ni *NetworkInterceptor) BlockPatterns(patterns ...string) *NetworkInterceptor
BlockPatterns blocks all requests matching the given URL glob patterns. Useful for blocking ads, trackers, and large resources. Example: "*analytics*", "*.ads.*", "*tracker*"
func (*NetworkInterceptor) MockJSON ¶ added in v0.2.0
func (ni *NetworkInterceptor) MockJSON(pattern string, status int, data interface{}) *NetworkInterceptor
MockJSON intercepts requests matching the pattern and returns a JSON response. This is the simplest way to mock an API endpoint:
interceptor.MockJSON(`/api/users`, 200, map[string]any{
"users": []map[string]any{{"id": 1, "name": "Alice"}},
})
func (*NetworkInterceptor) MockText ¶ added in v0.2.0
func (ni *NetworkInterceptor) MockText(pattern string, status int, body string, headerPairs ...string) *NetworkInterceptor
MockText intercepts requests matching the pattern and returns a text response.
func (*NetworkInterceptor) OnRequest ¶
func (ni *NetworkInterceptor) OnRequest(pattern string, handler func(req *InterceptedRequest)) *NetworkInterceptor
OnRequest adds a route that intercepts matching requests. The pattern uses CDP URL glob syntax: * matches any characters. Example: "*api/users*" matches any URL containing "api/users".
func (*NetworkInterceptor) Start ¶
func (ni *NetworkInterceptor) Start()
Start begins intercepting requests. Call Stop() when done.
func (*NetworkInterceptor) Stop ¶
func (ni *NetworkInterceptor) Stop() error
Stop stops intercepting requests.
type NetworkRoute ¶
type NetworkRoute struct {
// Pattern matches request URLs (regex).
Pattern string
// Handler processes matching requests.
Handler func(req *InterceptedRequest)
}
NetworkRoute defines a rule for intercepting network requests.
type OpenAIProvider ¶
type OpenAIProvider struct {
// contains filtered or unexported fields
}
OpenAIProvider implements LLMProvider for OpenAI-compatible APIs.
func NewOllama ¶
func NewOllama(model string) *OpenAIProvider
NewOllama creates an Ollama provider (OpenAI-compatible local inference).
func NewOpenAI ¶
func NewOpenAI(apiKey, model string) *OpenAIProvider
NewOpenAI creates an OpenAI provider.
func NewOpenAICompatible ¶
func NewOpenAICompatible(baseURL, apiKey, model string) *OpenAIProvider
NewOpenAICompatible creates a provider for OpenAI-compatible APIs (e.g., Ollama, vLLM).
func (*OpenAIProvider) ChatCompletion ¶
func (p *OpenAIProvider) ChatCompletion(ctx context.Context, messages []ChatMessage, opts ...ChatOption) (*ChatResponse, error)
func (*OpenAIProvider) Name ¶
func (p *OpenAIProvider) Name() string
type Page ¶
type Page struct {
// contains filtered or unexported fields
}
Page wraps a browser tab with high-level interaction methods.
func (*Page) AutoDismissDialogs ¶
func (p *Page) AutoDismissDialogs() func()
AutoDismissDialogs sets up automatic handling of JS dialogs. Alerts are accepted, confirms are accepted, prompts are dismissed. Returns a cancel function to stop auto-handling.
func (*Page) ClearGeolocation ¶ added in v0.2.0
ClearGeolocation removes the geolocation override.
func (*Page) DOMState ¶
DOMState extracts the current page state optimized for LLM consumption. This is the key method for AI agent integration.
func (*Page) DOMStateWithScreenshot ¶
DOMStateWithScreenshot extracts DOM state and captures a screenshot.
func (*Page) DeleteCookies ¶
DeleteCookies deletes cookies matching the given name.
func (*Page) DetectCAPTCHA ¶
func (p *Page) DetectCAPTCHA() (*CAPTCHAInfo, error)
DetectCAPTCHA inspects the current page for known CAPTCHA providers. Returns nil if no CAPTCHA is found.
func (*Page) DetectChallenge ¶ added in v0.5.0
func (p *Page) DetectChallenge() (ChallengeType, error)
DetectChallenge returns the bot-protection challenge currently shown on the page, or ChallengeNone if the page looks like real content.
func (*Page) ElementByXPath ¶
ElementByXPath finds an element by XPath expression.
func (*Page) EmulateDevice ¶ added in v0.2.0
EmulateDevice applies a device profile to the page (viewport, user agent, touch).
page.EmulateDevice(gosurfer.DeviceIPhoneX)
func (*Page) FocusedDOMState ¶ added in v0.2.3
FocusedDOMState extracts a pruned DOM state with boilerplate stripped. Removes nav, footer, cookie banners, ad containers, social links, and low-value links (terms, privacy, copyright, same-page anchors). Focuses on <main>, <article>, [role="main"] content regions. Typically produces 30-60% fewer tokens than DOMState.
func (*Page) FullScreenshot ¶
FullScreenshot captures the entire page (scrolled) as PNG bytes.
func (*Page) GetAllByRole ¶ added in v0.1.1
func (p *Page) GetAllByRole(role string, opts ...LocatorOption) ([]*Element, error)
GetAllByRole finds all elements matching the given ARIA role.
func (*Page) GetAllByText ¶ added in v0.1.1
func (p *Page) GetAllByText(text string, opts ...LocatorOption) ([]*Element, error)
GetAllByText finds all elements whose text content matches.
func (*Page) GetByAltText ¶ added in v0.1.1
func (p *Page) GetByAltText(text string, opts ...LocatorOption) (*Element, error)
GetByAltText finds the first element with a matching alt attribute.
func (*Page) GetByLabel ¶ added in v0.1.1
func (p *Page) GetByLabel(text string, opts ...LocatorOption) (*Element, error)
GetByLabel finds the first form element associated with a label matching the text. Checks <label for="...">, nested inputs inside <label>, and aria-label attributes.
func (*Page) GetByPlaceholder ¶ added in v0.1.1
func (p *Page) GetByPlaceholder(text string, opts ...LocatorOption) (*Element, error)
GetByPlaceholder finds the first element with a matching placeholder attribute.
func (*Page) GetByRole ¶ added in v0.1.1
func (p *Page) GetByRole(role string, opts ...LocatorOption) (*Element, error)
GetByRole finds the first element matching the given ARIA role. Supports both explicit role attributes and implicit roles from HTML semantics. Use Name("Submit") to filter by accessible name.
func (*Page) GetByTestID ¶ added in v0.1.1
GetByTestID finds the first element with a matching data-testid attribute.
func (*Page) GetByText ¶ added in v0.1.1
func (p *Page) GetByText(text string, opts ...LocatorOption) (*Element, error)
GetByText finds the first element whose text content matches. By default uses case-insensitive substring matching; use Exact() for exact match.
func (*Page) GetCookies ¶
GetCookies returns all cookies for the current page.
func (*Page) GetStorageState ¶ added in v0.1.1
func (p *Page) GetStorageState() (*StorageState, error)
GetStorageState captures the page's cookies and localStorage into a StorageState.
func (*Page) HandleDialog ¶
func (p *Page) HandleDialog() (wait func() *Dialog, handle func(accept bool, promptText string) error)
HandleDialog returns two functions: wait blocks until the next JS dialog opens, and handle accepts/dismisses it. Use for fine-grained dialog control.
func (*Page) HandleFileDialog ¶
HandleFileDialog prepares to handle a native file chooser dialog. Call before the action that triggers the file dialog, then invoke the returned function with the file paths to select.
func (*Page) HumanClick ¶ added in v0.2.8
HumanClick clicks an element with a random offset from center and a small delay, mimicking natural mouse behavior instead of clicking dead center instantly.
func (*Page) HumanMoveMouse ¶ added in v0.2.8
HumanMoveMouse moves the mouse along a curved path to the target coordinates, simulating natural hand movement using a Bezier-like curve.
func (*Page) HumanScroll ¶ added in v0.2.8
HumanScroll scrolls the page with natural-feeling incremental movements.
func (*Page) HumanType ¶ added in v0.2.8
HumanType types text character by character with random delays between keystrokes, mimicking natural typing speed (~50-150ms per character).
func (*Page) Intercept ¶
func (p *Page) Intercept() *NetworkInterceptor
Intercept sets up network request interception on the page.
func (*Page) KeyPress ¶
KeyPress sends a keyboard event (e.g., input.Enter, input.Escape, input.Tab).
func (*Page) LoadStorageState ¶ added in v0.1.1
LoadStorageState restores cookies and localStorage from a JSON file. The page should be navigated to the relevant origin before calling this.
page.Navigate("https://example.com")
page.LoadStorageState("auth.json")
func (*Page) LocalStorageAll ¶
LocalStorageAll returns all localStorage key-value pairs.
func (*Page) LocalStorageClear ¶
LocalStorageClear clears all localStorage.
func (*Page) LocalStorageDelete ¶
LocalStorageDelete removes a key from localStorage.
func (*Page) LocalStorageGet ¶
LocalStorageGet retrieves a value from localStorage.
func (*Page) LocalStorageSet ¶
LocalStorageSet stores a value in localStorage.
func (*Page) Navigate ¶
Navigate loads a URL and waits for the page to be ready. If the page is served a bot-protection challenge that can be auto-solved (e.g., Cloudflare's "Just a moment..." JavaScript challenge), Navigate will poll until the challenge clears or the configured timeout elapses.
Navigate does NOT return an error when the page lands on a non-auto-solvable challenge (Turnstile, DataDome) — the page did load, it just loaded a challenge. Callers who want to fail fast in that case should call DetectChallenge() after Navigate and check the return value. Only an auto-solvable challenge that fails to clear within the timeout produces an error.
func (*Page) RestoreStorageState ¶ added in v0.1.1
func (p *Page) RestoreStorageState(state *StorageState) error
RestoreStorageState applies a StorageState to the current page.
func (*Page) SaveStorageState ¶ added in v0.1.1
SaveStorageState serializes the page's cookies and localStorage to a JSON file. Use LoadStorageState to restore the state in a future session.
// After logging in:
page.SaveStorageState("auth.json")
func (*Page) Screenshot ¶
Screenshot captures the visible viewport as PNG bytes.
func (*Page) ScreenshotJPEG ¶
ScreenshotJPEG captures the viewport as JPEG bytes with the given quality (0-100).
func (*Page) Scroll ¶
Scroll scrolls the page by the given number of pixels. Positive dy scrolls down, negative scrolls up. Positive dx scrolls right, negative scrolls left.
func (*Page) ScrollToBottom ¶
ScrollToBottom scrolls to the bottom of the page.
func (*Page) ScrollToTop ¶
ScrollToTop scrolls to the top of the page.
func (*Page) SessionStorageGet ¶
SessionStorageGet retrieves a value from sessionStorage.
func (*Page) SessionStorageSet ¶
SessionStorageSet stores a value in sessionStorage.
func (*Page) SetColorScheme ¶ added in v0.2.0
func (p *Page) SetColorScheme(scheme ColorScheme) error
SetColorScheme emulates a CSS prefers-color-scheme media feature.
page.SetColorScheme(gosurfer.ColorSchemeDark)
func (*Page) SetCookies ¶
SetCookies sets multiple cookies at once.
func (*Page) SetGeolocation ¶ added in v0.2.0
SetGeolocation sets the geographic location for the page. Pass accuracy in meters (e.g., 100.0 for ~100m accuracy).
page.SetGeolocation(37.7749, -122.4194, 100) // San Francisco
func (*Page) SetLocale ¶ added in v0.2.0
SetLocale overrides the browser locale for Intl APIs (number formatting, date formatting, etc.). Use ICU locale format like "en_US", "fr_FR", "ja_JP". Note: this affects Intl.DateTimeFormat, Intl.NumberFormat, etc. but not navigator.language.
func (*Page) SetNetworkConditions ¶ added in v0.2.0
func (p *Page) SetNetworkConditions(latencyMs float64, downloadBytesPerSec, uploadBytesPerSec float64) error
SetNetworkConditions emulates network throttling. Latency is in milliseconds, throughput values are in bytes/second.
page.SetNetworkConditions(150, 1.6*1024*1024, 750*1024) // Regular 3G
func (*Page) SetOffline ¶ added in v0.2.0
SetOffline simulates offline/online network conditions.
page.SetOffline(true) // disconnect page.SetOffline(false) // reconnect
func (*Page) SetReducedMotion ¶ added in v0.2.0
func (p *Page) SetReducedMotion(motion ReducedMotion) error
SetReducedMotion emulates a CSS prefers-reduced-motion media feature.
func (*Page) SetTimezone ¶ added in v0.2.0
SetTimezone overrides the browser timezone. Use IANA timezone identifiers like "America/New_York", "Europe/London", "Asia/Tokyo".
func (*Page) SetTouchEnabled ¶ added in v0.2.0
SetTouchEnabled enables or disables touch event emulation.
func (*Page) SetUserAgent ¶ added in v0.2.0
SetUserAgent overrides the browser user agent string.
func (*Page) SetViewport ¶ added in v0.2.0
SetViewport overrides the page viewport dimensions and device scale factor.
page.SetViewport(375, 812, 3.0, true) // iPhone X
func (*Page) SolveCAPTCHA ¶
func (p *Page) SolveCAPTCHA(ctx context.Context, solver CAPTCHASolver) error
SolveCAPTCHA detects a CAPTCHA on the page and solves it using the provided solver. It injects the solution token into the page automatically.
func (*Page) StartHAR ¶
func (p *Page) StartHAR() *HARRecorder
StartHAR begins recording network traffic on the page. Call StopHAR() when done, then Export() to get the HAR data.
func (*Page) TargetID ¶
TargetID returns the CDP target ID for this page (used for tab tracking). Returns the last 4 chars as a short ID, matching Browser Use convention.
func (*Page) WaitForChallenge ¶ added in v0.5.0
WaitForChallenge polls the page until any auto-solvable challenge has cleared or the timeout elapses. It returns:
- the challenge type that was initially detected (ChallengeNone if no challenge was present)
- the time spent waiting
- an error ONLY if an auto-solvable challenge failed to clear in time
Non-auto-solvable challenges (Turnstile, DataDome) are returned without an error — the page did load, it just loaded a challenge. The caller can inspect the returned ChallengeType to decide what to do.
A timeout of 0 disables waiting (no-op). Callers should pick a value appropriate to the challenge — Cloudflare UAM typically resolves in 5-15 seconds, rarely longer.
func (*Page) WaitPopup ¶
WaitPopup waits for a new page/tab opened by this page (e.g., window.open, target="_blank"). Call before the action that triggers the popup.
func (*Page) WaitSelector ¶
WaitSelector waits for an element matching the selector to appear.
type PageExpect ¶ added in v0.1.1
type PageExpect struct {
// contains filtered or unexported fields
}
PageExpect provides auto-retrying page-level assertions.
func Expect ¶ added in v0.1.1
func Expect(page *Page, opts ...ExpectOption) *PageExpect
Expect creates auto-retrying assertions for a page. Assertions retry until they pass or the timeout expires (default 5s).
expect := gosurfer.Expect(page)
err := expect.ToHaveTitle("Dashboard")
err = expect.Locator("#btn").ToBeVisible()
func (*PageExpect) Locator ¶ added in v0.1.1
func (e *PageExpect) Locator(selector string) *LocatorExpect
Locator returns a LocatorExpect for assertions on a specific element.
func (*PageExpect) ToHaveTitle ¶ added in v0.1.1
func (e *PageExpect) ToHaveTitle(expected string) error
ToHaveTitle asserts the page title equals the expected string.
func (*PageExpect) ToHaveTitleContaining ¶ added in v0.1.1
func (e *PageExpect) ToHaveTitleContaining(substring string) error
ToHaveTitleContaining asserts the page title contains the substring.
func (*PageExpect) ToHaveURL ¶ added in v0.1.1
func (e *PageExpect) ToHaveURL(expected string) error
ToHaveURL asserts the page URL equals the expected string.
func (*PageExpect) ToHaveURLContaining ¶ added in v0.1.1
func (e *PageExpect) ToHaveURLContaining(substring string) error
ToHaveURLContaining asserts the page URL contains the substring.
type ParamDef ¶
type ParamDef struct {
Name string `json:"name"`
Type string `json:"type"` // "string", "int", "float", "bool"
Description string `json:"description"`
Required bool `json:"required"`
}
ParamDef describes a parameter for an action.
type ReducedMotion ¶ added in v0.2.0
type ReducedMotion string
ReducedMotion represents a CSS reduced-motion preference.
const ( ReducedMotionReduce ReducedMotion = "reduce" ReducedMotionNoPreference ReducedMotion = "" )
type Secrets ¶
type Secrets struct {
// contains filtered or unexported fields
}
Secrets manages sensitive data (credentials, TOTP secrets) for the agent. Keys ending in "_totp" are automatically treated as TOTP secrets and generate fresh codes on each access.
func NewSecrets ¶
NewSecrets creates a Secrets store from a key-value map.
func (*Secrets) Get ¶
Get retrieves a secret value. For keys ending in "_totp", a fresh TOTP code is generated from the stored secret.
func (*Secrets) ReplaceInText ¶
ReplaceInText replaces {{secret_name}} placeholders in text with actual secret values (generating TOTP codes for _totp keys).
type StepInfo ¶
type StepInfo struct {
Step int
Thought string
Action string
Params map[string]interface{}
Result string
Error error
Duration time.Duration
URL string
}
StepInfo provides information about a completed agent step.
type StorageState ¶ added in v0.1.1
type StorageState struct {
Cookies []Cookie `json:"cookies"`
LocalStorage map[string]string `json:"localStorage"`
Origin string `json:"origin"`
}
StorageState captures the full browser storage for a page: cookies and localStorage. It can be serialized to JSON for reuse across sessions (e.g., preserving login state).
type TokenUsage ¶
type TokenUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
TokenUsage tracks token consumption.
type TwoCaptchaSolver ¶
type TwoCaptchaSolver struct {
APIKey string
BaseURL string // default: https://2captcha.com
// contains filtered or unexported fields
}
TwoCaptchaSolver implements CAPTCHASolver using the 2captcha.com API.
func NewTwoCaptchaSolver ¶
func NewTwoCaptchaSolver(apiKey string) *TwoCaptchaSolver
NewTwoCaptchaSolver creates a 2Captcha solver.
func (*TwoCaptchaSolver) Name ¶
func (s *TwoCaptchaSolver) Name() string
func (*TwoCaptchaSolver) Solve ¶
func (s *TwoCaptchaSolver) Solve(ctx context.Context, info CAPTCHAInfo) (string, error)
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gosurfer
command
gosurfer CLI - persistent browser automation from the command line.
|
gosurfer CLI - persistent browser automation from the command line. |
|
gosurfer-mcp
command
Structured request logging with credential-safe URL sanitization.
|
Structured request logging with credential-safe URL sanitization. |
|
examples
|
|
|
benchmark
command
Benchmark: measures memory utilization of gosurfer during browser automation.
|
Benchmark: measures memory utilization of gosurfer during browser automation. |
|
scrape
command
Example: Direct browser automation (no AI) using gosurfer.
|
Example: Direct browser automation (no AI) using gosurfer. |
|
search
command
Example: AI-powered web search using gosurfer.
|
Example: AI-powered web search using gosurfer. |