chrome

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: ISC Imports: 19 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package chrome provides Chrome/Chromium browser automation for github.com/koizuka/scraper using chromedp.

It is a separate module so that programs using only the HTTP-based scraper do not depend on chromedp.

session := scraper.NewSession("session-name", logger)
chromeSession, cancel, err := chrome.New(session, chrome.Options{Headless: true})
defer cancel()
resp, err := chromeSession.RunNavigate("https://example.com")

Index

Constants

View Source
const (
	DefaultFilePermission = 0644 // Read/write for owner, read for group and others
	DefaultDirPermission  = 0755 // Read/write/execute for owner, read/execute for group and others
)

File permission constants

View Source
const (
	DefaultBlankURL = "about:blank"
)

URL constants for replay mode

Variables

This section is empty.

Functions

func NewTestContext

func NewTestContext(t *testing.T, timeout time.Duration) context.Context

NewTestContext creates an isolated Chrome browser context for tests. It uses a per-test temporary user data directory to avoid conflicts with other Chrome instances, and applies CI-aware timeouts. Retries up to 2 times on Chrome startup failure (flaky CI environments). Cleanup is registered via t.Cleanup, so callers don't need to defer cancel.

The user data directory is deliberately NOT t.TempDir(): its cleanup fails the test when files are still locked, and on Windows Chrome's crashpad handler (a separate process the allocator does not wait for) can briefly keep files open after the browser exits. Cleanup is best-effort instead.

func Unmarshal

func Unmarshal(ctx context.Context, v interface{}, cssSelector string, opt scraper.UnmarshalOption) error

Types

type DownloadFileOptions

type DownloadFileOptions struct {
	Timeout time.Duration
	Glob    string
}

type DownloadedFileNameNotSatisfiedError

type DownloadedFileNameNotSatisfiedError struct {
	DownloadedFilename string
	Glob               string
}

func (*DownloadedFileNameNotSatisfiedError) Error

type Options

type Options struct {
	Headless          bool
	Timeout           time.Duration
	ActionTimeout     time.Duration // Timeout for individual actions (0 = no timeout)
	ExtraAllocOptions []chromedp.ExecAllocatorOption
	UserDataDir       string // Chrome user data directory ("" = ./chromeUserData)
}

func NewTestOptions

func NewTestOptions(headless bool) Options

NewTestOptions creates Chrome options with CI compatibility built-in. This helper function simplifies test code by automatically including CI-compatible options when needed.

func NewTestOptionsWithTimeout

func NewTestOptionsWithTimeout(headless bool, timeout time.Duration) Options

NewTestOptionsWithTimeout creates Chrome options with CI compatibility and custom timeout. This helper function simplifies test code by automatically including CI-compatible options when needed, with a custom timeout setting.

type Session

type Session struct {
	*scraper.Session
	Ctx          context.Context // Operation context (may have timeout applied)
	BrowserCtx   context.Context // Browser context without timeout (for graceful shutdown)
	DownloadPath string

	ActionTimeout time.Duration // Timeout for individual actions (0 = no timeout)
	// contains filtered or unexported fields
}

Session holds the state for a Chrome browser session.

Ctx is the operation context, which may have a timeout applied. Use Ctx for all normal Chrome operations (navigation, clicks, queries).

BrowserCtx is the underlying browser context without timeout. Use BrowserCtx only for graceful shutdown via chromedp.Cancel(), which needs a live context even after session timeout:

defer func() {
    _ = chromedp.Cancel(chromeSession.BrowserCtx)
    cancel()
}()

func New

func New(session *scraper.Session, options Options) (chromeSession *Session, cancelFunc context.CancelFunc, err error)

New starts a Chrome browser bound to session and returns a Session. The returned cancel function must be called to shut down the browser.

func NewWithRetry

func NewWithRetry(session *scraper.Session, options Options, maxRetries int) (*Session, func(), error)

NewWithRetry creates a new Chrome session with retry logic for startup failures. This helper is specifically designed to handle flaky Chrome startup issues in CI environments. It retries on any Chrome startup failure (crashes, sandbox issues, websocket timeouts, etc.).

func (*Session) ActionCtx

func (session *Session) ActionCtx() (context.Context, context.CancelFunc)

ActionCtx returns a context with ActionTimeout applied if set. Use this for individual chromedp actions that should respect the action timeout. Caller must call the returned cancel function when done.

Example:

actionCtx, cancel := chromeSession.ActionCtx()
defer cancel()
err := chromedp.Do(actionCtx, chromedp.WaitVisible(chromedp.CSS(selector)))

func (*Session) ClearDebugStep

func (chromeSession *Session) ClearDebugStep()

ClearDebugStep implements UnifiedScraper.ClearDebugStep

func (*Session) Click

func (chromeSession *Session) Click(selector string) chromedp.Action[chromedp.Void]

Click returns an Action[chromedp.Void] that handles clicking with replay support

func (*Session) DoClick

func (chromeSession *Session) DoClick(selector string) error

DoClick implements UnifiedScraper.Click

func (*Session) DoNavigate

func (chromeSession *Session) DoNavigate(url string) error

DoNavigate implements UnifiedScraper.Navigate

func (*Session) DoSendKeys

func (chromeSession *Session) DoSendKeys(selector, value string) error

DoSendKeys implements UnifiedScraper.SendKeys

func (*Session) DoSleep

func (chromeSession *Session) DoSleep(duration time.Duration) error

DoSleep is a convenience method for executing Sleep action

func (*Session) DoWaitVisible

func (chromeSession *Session) DoWaitVisible(selector string) error

DoWaitVisible implements UnifiedScraper.WaitVisible

func (*Session) DownloadFile

func (session *Session) DownloadFile(filename *string, options DownloadFileOptions, actions ...chromedp.Action[chromedp.Void]) chromedp.Action[chromedp.Void]

func (*Session) DownloadResource

func (chromeSession *Session) DownloadResource(options scraper.UnifiedDownloadOptions) (string, error)

DownloadResource implements UnifiedScraper.DownloadResource

func (*Session) ExtractData

func (chromeSession *Session) ExtractData(v interface{}, selector string, opt scraper.UnmarshalOption) error

ExtractData implements UnifiedScraper.ExtractData

func (*Session) ExtractDataWithContext

func (chromeSession *Session) ExtractDataWithContext(ctx context.Context, v interface{}, selector string, opt scraper.UnmarshalOption) error

ExtractDataWithContext implements scraper.ContextExtractor

func (*Session) FollowAnchor

func (chromeSession *Session) FollowAnchor(text string) error

FollowAnchor implements UnifiedScraper.FollowAnchor

func (*Session) GetCurrentURL

func (session *Session) GetCurrentURL() (string, error)

GetCurrentURL returns the current page URL

func (*Session) GetDebugStep

func (chromeSession *Session) GetDebugStep() string

GetDebugStep implements UnifiedScraper.GetDebugStep

func (*Session) Navigate

func (chromeSession *Session) Navigate(url string) chromedp.Action[chromedp.Void]

Navigate returns an Action[chromedp.Void] that handles navigation with replay support

func (*Session) Printf

func (chromeSession *Session) Printf(format string, a ...interface{})

Printf implements UnifiedScraper.Printf

func (*Session) RunNavigate

func (session *Session) RunNavigate(URL string) (*network.Response, error)

RunNavigate navigates to page URL and download html like scraper.Session.invoke

func (*Session) SaveFile

func (session *Session) SaveFile(filename *string) chromedp.Action[chromedp.Void]

*

  • SaveFile saves file to filename
  • filename: DownloadFile の結果を chromedp.Do で続ける場合、ポインタにしないと実行前の値が渡ってしまうため、ポインタにする

func (*Session) SaveHtml

func (session *Session) SaveHtml(filename *string) chromedp.Action[chromedp.Void]

func (*Session) SaveLastHtmlSnapshot

func (session *Session) SaveLastHtmlSnapshot() error

SaveLastHtmlSnapshot saves the last HTML content to a timestamped snapshot file This is useful for debugging timeouts - call this when an error occurs Multiple snapshots are preserved to track timeout sequences

func (*Session) SavePage

func (chromeSession *Session) SavePage() (string, error)

SavePage implements UnifiedScraper.SavePage (calls existing SaveHtml action)

func (*Session) ScraperType

func (chromeSession *Session) ScraperType() scraper.ScraperType

ScraperType implements scraper.ScraperTyper

func (*Session) SendKeys

func (chromeSession *Session) SendKeys(selector, value string) chromedp.Action[chromedp.Void]

SendKeys returns an Action[chromedp.Void] that handles sending keys with replay support

func (*Session) SetDebugStep

func (chromeSession *Session) SetDebugStep(step string)

SetDebugStep implements UnifiedScraper.SetDebugStep

func (*Session) Sleep

func (chromeSession *Session) Sleep(duration time.Duration) chromedp.Action[chromedp.Void]

Sleep returns an Action[chromedp.Void] that handles sleeping with replay support. In replay mode, this method returns immediately without actual waiting, which speeds up test execution. In record mode, this method performs the actual sleep operation using chromedp.Sleep.

func (*Session) SubmitForm

func (chromeSession *Session) SubmitForm(formSelector string, params map[string]string) error

SubmitForm implements UnifiedScraper.SubmitForm

func (*Session) Unmarshal

func (session *Session) Unmarshal(v interface{}, cssSelector string, opt scraper.UnmarshalOption) error

func (*Session) WaitVisible

func (chromeSession *Session) WaitVisible(selector string) chromedp.Action[chromedp.Void]

WaitVisible returns an Action[chromedp.Void] that handles wait visible with replay support

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL