ei0haro
ei0haro

Reputation: 91

How to use a selector finding an frame (iframe) using Playwright

I have a trivial question that I can't find an answer to using Microsoft Playwright framework. According to documentation you can fetch an iframe with the following code:

const frame = page.frame('frame-login');

But how do I use a selector to find and interact with an iframe? I need to use a CSS selector to find my iframe since it does not have an id.

Any help appreciated

Upvotes: 9

Views: 20623

Answers (2)

theDavidBarton
theDavidBarton

Reputation: 8851

You can use elementHandle.contentFrame()

await page.waitForSelector('.class-name')

const elementHandle = await page.$('.class-name')
const frame = await elementHandle.contentFrame()

From that moment you can interact with the content of the <iframe> like: await frame.<method_name>.

Upvotes: 11

hardkoded
hardkoded

Reputation: 21695

You can get the ElementHandle calling $ and then call the contentFrame function:

const handle = await page.$('.frame');
const contentFrame = await handle.contentFrame();

Upvotes: 2

Related Questions