Frames, Shadow DOM & Scoping

Switch To Frame

Switches the execution context into the matched iframe, so following selectors are evaluated within that frame. Call it with the iframe's own selector first, then select the elements inside it, and use InMainDom to come back out. Mandatory parameter: elementId.

WARNING

Puppeteer only. OttoMagic runs inside every frame already, so it has nothing to switch to, and Selenium does its own frame switching through the driver. The GPAL-level InFrame works on all three; only this route is engine-specific.

Examples

GPAL Fluent: High-level fluent C# API

//The browser-level call is InFrame, taking a Selector.

browser

.InFrame(iframeSelector)

.WithSelector(childSelector)

.LeftClick();


browser.InMainDom();

MagicHelper: MagicHelper utility library

//InFrame calls SwitchToElement underneath, and that is the whole implementation. OttoMagic runs a content script in every frame, so it is already inside the iframe and has nothing to switch into; scoping to the iframe element gets the same result. There is no switch-to-frame route on this layer for the same reason, which is why the REST column is empty.

browser.MagicHelper.InFrame(string elementId);

Puppeteer Client: Puppeteer-style C# client

//Puppeteer is the only engine that switches frames, because it is the only one not already running inside them.

browser.PuppeteerClient.SwitchToFrame("#iframe-selector").Execute();

Puppeteer Communicator: Low-level communicator

//Resolves the iframe element, attaches to its frame target so a cross origin frame is reachable, and gives it an isolated world. Everything after it runs inside the iframe until the context is put back.

await browser.PuppeteerCommunicator.SwitchToFrame(string frameSelector);

REST Client: Direct REST/HTTP client

//The switch-to-frame route is Puppeteer only. Over REST the equivalent is switch-to-element, scoping to the iframe element, which is what MagicHelper.InFrame sends.

browser.OttoMagicClient.SwitchToElement(string elementId).Execute();

💬 Ask GPAL