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
- func NewTestContext(t *testing.T, timeout time.Duration) context.Context
- func Unmarshal(ctx context.Context, v interface{}, cssSelector string, ...) error
- type DownloadFileOptions
- type DownloadedFileNameNotSatisfiedError
- type Options
- type Session
- func (session *Session) ActionCtx() (context.Context, context.CancelFunc)
- func (chromeSession *Session) ClearDebugStep()
- func (chromeSession *Session) Click(selector string) chromedp.Action[chromedp.Void]
- func (chromeSession *Session) DoClick(selector string) error
- func (chromeSession *Session) DoNavigate(url string) error
- func (chromeSession *Session) DoSendKeys(selector, value string) error
- func (chromeSession *Session) DoSleep(duration time.Duration) error
- func (chromeSession *Session) DoWaitVisible(selector string) error
- func (session *Session) DownloadFile(filename *string, options DownloadFileOptions, ...) chromedp.Action[chromedp.Void]
- func (chromeSession *Session) DownloadResource(options scraper.UnifiedDownloadOptions) (string, error)
- func (chromeSession *Session) ExtractData(v interface{}, selector string, opt scraper.UnmarshalOption) error
- func (chromeSession *Session) ExtractDataWithContext(ctx context.Context, v interface{}, selector string, ...) error
- func (chromeSession *Session) FollowAnchor(text string) error
- func (session *Session) GetCurrentURL() (string, error)
- func (chromeSession *Session) GetDebugStep() string
- func (chromeSession *Session) Navigate(url string) chromedp.Action[chromedp.Void]
- func (chromeSession *Session) Printf(format string, a ...interface{})
- func (session *Session) RunNavigate(URL string) (*network.Response, error)
- func (session *Session) SaveFile(filename *string) chromedp.Action[chromedp.Void]
- func (session *Session) SaveHtml(filename *string) chromedp.Action[chromedp.Void]
- func (session *Session) SaveLastHtmlSnapshot() error
- func (chromeSession *Session) SavePage() (string, error)
- func (chromeSession *Session) ScraperType() scraper.ScraperType
- func (chromeSession *Session) SendKeys(selector, value string) chromedp.Action[chromedp.Void]
- func (chromeSession *Session) SetDebugStep(step string)
- func (chromeSession *Session) Sleep(duration time.Duration) chromedp.Action[chromedp.Void]
- func (chromeSession *Session) SubmitForm(formSelector string, params map[string]string) error
- func (session *Session) Unmarshal(v interface{}, cssSelector string, opt scraper.UnmarshalOption) error
- func (chromeSession *Session) WaitVisible(selector string) chromedp.Action[chromedp.Void]
Constants ¶
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
const (
DefaultBlankURL = "about:blank"
)
URL constants for replay mode
Variables ¶
This section is empty.
Functions ¶
func NewTestContext ¶
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.
Types ¶
type DownloadFileOptions ¶
type DownloadedFileNameNotSatisfiedError ¶
func (*DownloadedFileNameNotSatisfiedError) Error ¶
func (e *DownloadedFileNameNotSatisfiedError) Error() string
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 ¶
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 ¶
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 ¶
Click returns an Action[chromedp.Void] that handles clicking with replay support
func (*Session) DoNavigate ¶
DoNavigate implements UnifiedScraper.Navigate
func (*Session) DoSendKeys ¶
DoSendKeys implements UnifiedScraper.SendKeys
func (*Session) DoWaitVisible ¶
DoWaitVisible implements UnifiedScraper.WaitVisible
func (*Session) DownloadFile ¶
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 ¶
FollowAnchor implements UnifiedScraper.FollowAnchor
func (*Session) GetCurrentURL ¶
GetCurrentURL returns the current page URL
func (*Session) GetDebugStep ¶
GetDebugStep implements UnifiedScraper.GetDebugStep
func (*Session) Navigate ¶
Navigate returns an Action[chromedp.Void] that handles navigation with replay support
func (*Session) RunNavigate ¶
RunNavigate navigates to page URL and download html like scraper.Session.invoke
func (*Session) SaveFile ¶
*
- SaveFile saves file to filename
- filename: DownloadFile の結果を chromedp.Do で続ける場合、ポインタにしないと実行前の値が渡ってしまうため、ポインタにする
func (*Session) SaveLastHtmlSnapshot ¶
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 ¶
SavePage implements UnifiedScraper.SavePage (calls existing SaveHtml action)
func (*Session) ScraperType ¶
func (chromeSession *Session) ScraperType() scraper.ScraperType
ScraperType implements scraper.ScraperTyper
func (*Session) SendKeys ¶
SendKeys returns an Action[chromedp.Void] that handles sending keys with replay support
func (*Session) SetDebugStep ¶
SetDebugStep implements UnifiedScraper.SetDebugStep
func (*Session) Sleep ¶
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 ¶
SubmitForm implements UnifiedScraper.SubmitForm