Basic Concepts

Waiting: Before an Element vs After an Action

WaitFor After WithSelector: Waiting For Something to Appear

When WaitFor follows one or more WithSelector calls and comes before an action, it tells GPAL how long to keep searching for those elements before giving up -- WaitFor(5_000) searches for up to five seconds, and with no WaitFor at all GPAL searches once. If the elements appear within that window, the workflow continues normally. If they don't, GPAL invokes CallIfNotFound (if one is registered) instead of throwing -- this is the mechanism behind 'wait for the page to finish loading this widget' style waits.

var widgetText = GPAL.Grid.ToGPALObject();

browser

.GoTo("https://example.com")

.WithSelector((Selector)"#late-loading-widget")

.WaitFor(5_000) // search for up to 5 seconds

.CallIfNotFound(LogMissingWidget)

.GetGrid(ref widgetText);

WaitFor After an Action: Just a Sleep

When WaitFor follows an action -- a click, a fill-in, a navigation -- there's nothing left to search for, so WaitFor simply pauses the workflow for the given duration. This is a plain delay: useful for giving a page time to react to a click before the next step runs, but it doesn't check for anything and won't invoke CallIfNotFound.

var results = GPAL.Grid.ToGPALObject();

browser

.LeftClick(searchButton)

.WaitFor(2_000) // just a pause - nothing is being searched for

.WithSelector(resultsGrid)

.GetGrid(ref results);

NOTE

GPAL deliberately reuses WaitFor for both cases rather than introducing a second method name. The description is literally 'if used after WithSelector, this is a find timeout; if used after an action, this is a sleep timer.' Reading the line above WaitFor tells you which behavior applies.

Other WaitFor Overloads

WaitFor(Selector) looks for a specific selector as a step of its own. It takes no time, so it searches once, or for as long as a global GPAL.WithWaitFor allows; for a timed wait use WithSelector(...).WaitFor(ms). WaitFor(ElementState) waits for the elements already defined by WithSelector to reach a particular state (such as becoming visible or enabled) rather than just existing in the DOM.

// Look for a specific selector once, or as long as GPAL.WithWaitFor allows

browser.WaitFor((Selector)GPAL.Selector.WithCSS("#confirmation-banner"));


// Wait for already-selected elements to reach a state

browser

.WithSelector(submitButton)

.WaitFor(ElementState.Enabled)

.LeftClick();

💬 Ask GPAL