Interaction

Focus

Focuses onto a web element, clicking first if it's editable.

Focuses onto a web element, clicking first if it's editable.

The specified element becomes the active element, allowing keyboard input to be directed to that element. If the element is editable (an INPUT of type text/email/password/search, a TEXTAREA, a contenteditable element, or anything with a text caret), Focus performs a real click on it - this both focuses the element and places the text caret, getting it ready for typing. Non-editable elements are simply focused/moved-to without a click, avoiding side effects like triggering a link navigation or button action.

TIP

This editable-vs-not branch (via IsElementEditable) is applied consistently across every engine - Hardware, OttoMagic, Puppeteer, and Selenium all click-to-focus editable elements and move-to/focus non-editable ones the same way. It replaces an older Selenium-only special case that sent a Shift keypress to <input> elements to focus them.

Examples

GPAL Fluent: High-level fluent C# API

//For editable elements this is equivalent to a LeftClick (focus + caret placement); for everything else it's equivalent to MoveTo/Hover (focus without a click).

browser.WithSelector(selector).Focus() browser.Focus(selector)

MagicHelper: MagicHelper utility library

GPAL.MagicHelper.Focus(string css); GPAL.MagicHelper.Focus(string xpath);

Puppeteer Client: Puppeteer-style C# client

browser.PuppeteerClient.Focus(string css).Execute(); browser.PuppeteerClient.Focus(string xpath).Execute();

Puppeteer Communicator: Low-level communicator

//PuppeteerCommunicator's Focus takes a resolved backendNodeId rather than a raw CSS/XPath selector. For editable elements, GPAL's editable-aware Focus calls the LeftClick path instead (which focuses and places the caret); this DOM.focus-based Focus is used for non-editable elements.

await browser.PuppeteerCommunicator.Focus(int backendNodeId, string sessionId);

REST Client: Direct REST/HTTP client

GPAL.OttoMagicClient.WithEndpoint(ApiEndpoint.Focus).WithCss(string css).Execute(); GPAL.OttoMagicClient.WithEndpoint(ApiEndpoint.Focus).WithXpath(string xpath).Execute();