Automation Targets

Waiting: Before an Element vs After an Action

WaitFor means two different things depending on where it appears in a workflow: after WithSelector it's a search timeout for elements that haven't appeared yet; after an action it's a plain sleep. Knowing which one you're calling matters.

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 -- the default is three seconds if you call WaitFor with no argument. 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.

browser

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

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

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

.CallIfNotFound(LogMissingWidget)

.GetGrid(out var 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.

browser

.LeftClick(searchButton)

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

.WithSelector(resultsGrid)

.GetGrid(out var results);

TIP

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) waits for a specific selector to appear, defaulting to three seconds if no time is given. Functionally similar to WithSelector(...).WaitFor(ms) but as a single call. 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.

// Wait for a specific selector to show up, default 3-second timeout

browser.WaitFor("#confirmation-banner");

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

browser

.WithSelector(submitButton)

.WaitFor(ElementState.Enabled)

.LeftClick();