Documentation
¶
Overview ¶
Package scenario provides a fluent builder for creating Scrapfly API JS Scenarios.
It abstracts away the complexity of building the raw JSON structure (`[]map[string]interface{}`), providing a type-safe and user-friendly interface. The final output is a base64 encoded string, ready to be used as the `js_scenario` API parameter.
Example Usage ¶
import "your/project/path/scenario"
func main() {
// Create a new scenario builder and chain actions.
sc, err := scenario.New().
// Fill in login credentials
Fill("input[name=username]", "user123").
Fill("input[name=password]", "password", scenario.WithFillClear(true)).
// Click the login button
Click("button[type='submit']").
// Wait for the page to navigate after login
WaitForNavigation(scenario.WithNavTimeout(5000)).
// Wait for a specific element to appear on the new page
WaitForSelector("#dashboard", scenario.WithSelectorTimeout(10000)).
// Execute a script to get the user agent
Execute("return navigator.userAgent").
// Finalize the builder to get the base64 encoded string.
Build()
if err != nil {
// Handle error
log.Fatalf("Failed to build scenario: %v", err)
}
// Now `sc` can be passed directly to the Scrapfly API client.
scrapeConfig := &scrapfly.ScrapeConfig{
URL: "https://web-scraping.dev",
RenderJS: true,
Country: "us",
Unblocker: scrapfly.BoolPtr(true),
JSScenario: sc,
}
}
Index ¶
- Variables
- type ClickOption
- type ConditionAction
- type ExecuteOption
- type FillOption
- type JSScenarioStep
- type ScenarioBuilder
- func (b *ScenarioBuilder) Build() ([]JSScenarioStep, error)
- func (b *ScenarioBuilder) Click(selector string, opts ...ClickOption) *ScenarioBuilder
- func (b *ScenarioBuilder) ConditionOnSelector(selector string, state SelectorState, action ConditionAction) *ScenarioBuilder
- func (b *ScenarioBuilder) ConditionOnStatusCode(statusCode int, action ConditionAction) *ScenarioBuilder
- func (b *ScenarioBuilder) Execute(script string, opts ...ExecuteOption) *ScenarioBuilder
- func (b *ScenarioBuilder) Fill(selector, value string, opts ...FillOption) *ScenarioBuilder
- func (b *ScenarioBuilder) Scroll(opts ...ScrollOption) *ScenarioBuilder
- func (b *ScenarioBuilder) SolveCaptcha(opts ...SolveCaptchaOption) *ScenarioBuilder
- func (b *ScenarioBuilder) Steps() []JSScenarioStep
- func (b *ScenarioBuilder) Wait(milliseconds int) *ScenarioBuilder
- func (b *ScenarioBuilder) WaitForNavigation(opts ...WaitForNavOption) *ScenarioBuilder
- func (b *ScenarioBuilder) WaitForSelector(selector string, opts ...WaitForSelectorOption) *ScenarioBuilder
- type ScrollOption
- type SelectorState
- type SolveCaptchaOption
- type WaitForNavOption
- type WaitForSelectorOption
Constants ¶
This section is empty.
Variables ¶
var JsScenarioSchema *jsonschema.Schema
JsScenarioSchema is the schema for the JS Scenario. Use it with more capable models or where compatibility with recent meta-schemas is required.
var JsScenarioSchemaFlattened *jsonschema.Schema
JsScenarioSchemaFlattened is the flattened schema for the JS Scenario. Use it with more "humble" models or where compatibility with recent meta-schemas if not available.
Functions ¶
This section is empty.
Types ¶
type ClickOption ¶
type ClickOption func(*clickParams)
ClickOption is a function that configures a click action.
func WithClickIgnoreIfNotVisible ¶
func WithClickIgnoreIfNotVisible(ignore bool) ClickOption
WithClickIgnoreIfNotVisible sets the 'ignore_if_not_visible' option for a click. If true, the step will be skipped if the element is not visible.
func WithClickMultiple ¶
func WithClickMultiple(multiple bool) ClickOption
WithClickMultiple sets the 'multiple' option for a click. If true, it will click on all elements matching the selector.
type ConditionAction ¶
type ConditionAction string
ConditionAction defines the behavior when a condition is met.
const ( // ActionContinue continues the scenario if the condition is met. ActionContinue ConditionAction = "continue" // ActionExitSuccess stops the scenario and marks it as successful if the condition is met. ActionExitSuccess ConditionAction = "exit_success" // ActionExitFailed stops the scenario and marks it as failed if the condition is met. ActionExitFailed ConditionAction = "exit_failed" )
type ExecuteOption ¶
type ExecuteOption func(*executeParams)
ExecuteOption is a function that configures an execute action.
func WithExecuteTimeout ¶
func WithExecuteTimeout(milliseconds int) ExecuteOption
WithExecuteTimeout sets the timeout for a script execution.
type FillOption ¶
type FillOption func(*fillParams)
FillOption is a function that configures a fill action.
func WithFillClear ¶
func WithFillClear(clear bool) FillOption
WithFillClear sets the 'clear' option for a fill action. If true, the input field will be cleared before typing the value.
type JSScenarioStep ¶
JSScenarioStep represents a single step in the JS scenario.
type ScenarioBuilder ¶
type ScenarioBuilder struct {
// contains filtered or unexported fields
}
ScenarioBuilder manages the construction of a JS scenario.
func (*ScenarioBuilder) Build ¶
func (b *ScenarioBuilder) Build() ([]JSScenarioStep, error)
Build finalizes the scenario, ensure it has no errors and returns the steps. If any errors occurred during the building process, they will be returned here. If the scenario is empty, it returns nil and no error.
func (*ScenarioBuilder) Click ¶
func (b *ScenarioBuilder) Click(selector string, opts ...ClickOption) *ScenarioBuilder
Click adds a step to click on an element matching the given CSS or XPath selector.
func (*ScenarioBuilder) ConditionOnSelector ¶
func (b *ScenarioBuilder) ConditionOnSelector(selector string, state SelectorState, action ConditionAction) *ScenarioBuilder
ConditionOnSelector adds a condition step based on the presence or absence of an element.
func (*ScenarioBuilder) ConditionOnStatusCode ¶
func (b *ScenarioBuilder) ConditionOnStatusCode(statusCode int, action ConditionAction) *ScenarioBuilder
ConditionOnStatusCode adds a condition step that checks the HTTP status code of the response.
func (*ScenarioBuilder) Execute ¶
func (b *ScenarioBuilder) Execute(script string, opts ...ExecuteOption) *ScenarioBuilder
Execute adds a step to run a custom JavaScript snippet in the browser context.
func (*ScenarioBuilder) Fill ¶
func (b *ScenarioBuilder) Fill(selector, value string, opts ...FillOption) *ScenarioBuilder
Fill adds a step to type a text value into an element matching the selector.
func (*ScenarioBuilder) Scroll ¶
func (b *ScenarioBuilder) Scroll(opts ...ScrollOption) *ScenarioBuilder
Scroll adds a step to scroll the page or a specific element. By default (with no options), it scrolls to the bottom of the page.
func (*ScenarioBuilder) SolveCaptcha ¶ added in v1.7.0
func (b *ScenarioBuilder) SolveCaptcha(opts ...SolveCaptchaOption) *ScenarioBuilder
SolveCaptcha adds a step that solves the captcha on the page. A scenario holds 5 of these steps at most.
func (*ScenarioBuilder) Steps ¶
func (b *ScenarioBuilder) Steps() []JSScenarioStep
Steps returns the steps of the scenario. Use this method when passing the scenario to the ScrapeConfig.JSScenario field.
func (*ScenarioBuilder) Wait ¶
func (b *ScenarioBuilder) Wait(milliseconds int) *ScenarioBuilder
Wait adds a step to pause the scenario for a specified duration.
func (*ScenarioBuilder) WaitForNavigation ¶
func (b *ScenarioBuilder) WaitForNavigation(opts ...WaitForNavOption) *ScenarioBuilder
WaitForNavigation adds a step that waits for a page navigation to complete. This is useful after actions that trigger a page load, like clicking a link or submitting a form.
func (*ScenarioBuilder) WaitForSelector ¶
func (b *ScenarioBuilder) WaitForSelector(selector string, opts ...WaitForSelectorOption) *ScenarioBuilder
WaitForSelector adds a step that waits for an element to appear, disappear, or reach a specific state.
type ScrollOption ¶
type ScrollOption func(*scrollParams)
ScrollOption is a function that configures a scroll action.
func WithScrollClickAfter ¶
func WithScrollClickAfter(selector string) ScrollOption
WithScrollClickAfter performs a click on the given selector after each scroll, useful for "load more" buttons in infinite scroll scenarios.
func WithScrollElement ¶
func WithScrollElement(selector string) ScrollOption
WithScrollElement sets a container element (via selector) within which to scroll. Defaults to the page body.
func WithScrollInfinite ¶
func WithScrollInfinite(iterations int) ScrollOption
WithScrollInfinite performs a specified number of "infinite scrolls" to the bottom.
func WithScrollToSelector ¶
func WithScrollToSelector(selector string) ScrollOption
WithScrollToSelector sets a target element (via selector) to scroll to. Can also be "bottom".
type SelectorState ¶
type SelectorState string
SelectorState defines the desired state of an element to wait for.
const ( // SelectorStateVisible waits for the element to be present and visible in the DOM. SelectorStateVisible SelectorState = "visible" // SelectorStateHidden waits for the element to be removed or hidden from the DOM. SelectorStateHidden SelectorState = "hidden" )
type SolveCaptchaOption ¶ added in v1.7.0
type SolveCaptchaOption func(*solveCaptchaParams)
SolveCaptchaOption is a function that configures a solve_captcha action.
func WithCaptchaIgnore ¶ added in v1.7.0
func WithCaptchaIgnore() SolveCaptchaOption
WithCaptchaIgnore continues the scenario when the captcha is not solved.
func WithCaptchaTimeout ¶ added in v1.7.0
func WithCaptchaTimeout(milliseconds int) SolveCaptchaOption
WithCaptchaTimeout sets the maximum time to wait for the captcha to be solved.
type WaitForNavOption ¶
type WaitForNavOption func(*waitForNavParams)
WaitForNavOption is a function that configures a wait_for_navigation action.
func WithNavTimeout ¶
func WithNavTimeout(milliseconds int) WaitForNavOption
WithNavTimeout sets the maximum time to wait for a navigation to occur.
type WaitForSelectorOption ¶
type WaitForSelectorOption func(*waitForSelectorParams)
WaitForSelectorOption is a function that configures a wait_for_selector action.
func WithSelectorState ¶
func WithSelectorState(state SelectorState) WaitForSelectorOption
WithSelectorState sets the desired state of the element to wait for (visible or hidden).
func WithSelectorTimeout ¶
func WithSelectorTimeout(milliseconds int) WaitForSelectorOption
WithSelectorTimeout sets the maximum time to wait for the selector to reach the desired state.