Browser

Waiting for Elements, State, and Time (WaitFor)

WaitFor has three overloads on Browser - a fixed time, a Selector, or an ElementState - and behaves differently depending on whether it is called before or after an action in the current unit of work.

WaitFor has three overloads on Browser - a fixed time, a Selector, or an ElementState - and behaves differently depending on whether it is called before or after an action in the current unit of work.

WaitFor(WaitTime) is dual-purpose: if called AFTER an action (LeftClick, FillInFrom, GoTo, etc.) it is a plain Thread.Sleep for that many milliseconds; if called BEFORE an action - right after WithSelector, with no action yet performed in this unit of work - it sets the maximum time GPAL will wait for the WithSelector elements to appear before continuing (CallIfNotFound fires if nothing is found in time). WaitTime accepts any integer number of milliseconds, or one of three named values: WaitTime.Immediate (0, the default), WaitTime.Forever (-1, wait indefinitely for the selector), or WaitTime.Never (-2, do not wait/check for the selector at all). WaitFor(Selector) waits specifically for the given selector to be present on the page before continuing, independent of (and in addition to) the selectors already added with WithSelector - useful for waiting on a 'page is ready' marker before scraping a different element. WaitFor(ElementState) waits for the element(s) defined by the current WithSelector to reach the given ElementState (Visible, Hidden, Enabled, Disabled, Clickable, Editable, Checked, Selected, etc.) before continuing.

TIP

GPAL.WithWaitFor(int waitForInTicks) sets the default selector-wait timeout (in milliseconds) applied to every unit of work in every workflow - both Browser and Application. Calling .WaitFor(...) on a specific unit of work overrides this global default for that unit of work only.

WARNING

If WaitFor(ElementState) is used without a positive wait time already set for the unit of work (no prior .WaitFor(timeInMs) and no GPAL.WithWaitFor default), GPAL logs a WARNING event explaining this and falls back to a 3000ms (3 second) poll for the state. Set an explicit .WaitFor(timeInMs) first if you need a different timeout.

Examples

GPAL Fluent: High-level fluent C# API

//Whether WaitFor(WaitTime) sleeps or waits-for-selector depends entirely on whether an action has already been called in the current unit of work - put it immediately after WithSelector (before any action) to wait for elements, or after an action to pause the workflow. WaitFor(Selector) and WaitFor(ElementState) are always actions in their own right and run immediately.

// 1) WaitFor(WaitTime) BEFORE an action - wait up to 5s for the selector to appear GPAL.Browser .GoTo("https://example.com") .WithSelector(".results-loaded") .WaitFor(5000) .WithSelector(".result-row") .GetGrid(out var results); // 1b) WaitFor(WaitTime) AFTER an action - plain sleep for 1.5s GPAL.Browser .WithSelector("#submit") .LeftClick() .WaitFor(1500); // 1c) WaitFor(WaitTime.Forever) - wait indefinitely for a slow page GPAL.Browser .GoTo("https://example.com/slow-report") .WithSelector("#report-ready") .WaitFor(WaitTime.Forever) .GetPageSource(out var html); // 2) WaitFor(Selector) - wait for a different element than the one you're about to scrape GPAL.Browser .GoTo("https://example.com/dashboard") .WaitFor("#dashboard-loaded") .WithSelector(".widget-value") .GetGrid(out var widgets); // 3) WaitFor(ElementState) - wait until a button becomes clickable GPAL.Browser .GoTo("https://example.com/checkout") .WithSelector("#place-order") .WaitFor(ElementState.Clickable) .LeftClick();