Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Playwright Frames and New Windows

Playwright Frames and New Windows

This chapter covers how to handle iframes, popup dialogs, new tabs, and Playwright's event system.


iframe operations

Playwright uses frameLocator() to locate and operate on elements inside an iframe.

page.frameLocator() to locate iframes

Example

test

(

'Operate on elements inside an iframe'

,

async

(

{

page

}

)

=>

{

  await page.

goto

(

'https://www.runoob.com/iframe-demo'

)

;

 

// Locate the iframe, then locate elements within it

 

const

iframe

=

page.

frameLocator

(

'#my-iframe'

)

;

 

// Operate on elements inside the iframe

  await iframe.

getByRole

(

'button'

,

{

name

:

'Submit'

}

)

.

click

(

)

;

  await iframe.

getByLabel

(

'Username'

)

.

fill

(

'runoob_user'

)

;

 

// Assert inside the iframe

  await expect

(

iframe.

getByText

(

'Operation successful'

)

)

.

toBeVisible

(

)

;

}

)

;

Get all Frames

Example

// Get all frames on the page (including the main frame and iframes)

const

frames

=

page.

frames

(

)

;

console.

log

(

'The page has a total of'

,

frames.

length

,

'frames'

)

;

frames.

forEach

(

frame

=>

{

  console.

log

(

'Frame:'

,

frame.

name

(

)

,

frame.

url

(

)

)

;

}

)

;

Nested iframes

Playwright's frameLocator() supports chaining, allowing you to drill into deeply nested iframes.

Example

// Nested iframe: outer → inner → element

const

innerButton

=

page

  .

frameLocator

(

'#outer-frame'

)

  .

frameLocator

(

'#inner-frame'

)

  .

getByRole

(

'button'

,

{

name

:

'Confirm'

}

)

;

await innerButton.

click

(

)

;


Dialog handling

Playwright has default handling strategies for native browser dialogs (alert, confirm, prompt).

Default behavior

By default, Playwright automatically dismisses all dialogs:

Dialog type Default behavior
alert() Auto-accept (close)
confirm() Auto-accept (returns true)
prompt() Auto-accept (returns empty string)

Custom dialog handling

Pass page.on('dialog') You can intercept and customize dialog handling.

Example

test

(

'Custom dialog handling'

,

async

(

{

page

}

)

=>

{

 

// Register a listener before the dialog is triggered

  page.

on

(

'dialog'

,

async dialog

=>

{

    console.

log

(

'Dialog type:'

,

dialog.

type

(

)

)

;

    console.

log

(

'Dialog message:'

,

dialog.

message

(

)

)

;

   

if

(

dialog.

type

(

)

===

'confirm'

)

{

     

// Dismiss the confirm dialog

      await dialog.

dismiss

(

)

;

   

}

else

if

(

dialog.

type

(

)

===

'prompt'

)

{

     

// Enter custom content

      await dialog.

accept

(

'Content entered by RUNOOB'

)

;

   

}

else

{

     

// Accept other types directly

      await dialog.

accept

(

)

;

   

}

 

}

)

;

 

// Action that triggers the dialog

  await page.

getByRole

(

'button'

,

{

name

:

'Delete'

}

)

.

click

(

)

;

}

)

;

Dialog Object Methods

Method Description
dialog.accept(promptText?) Accept dialog (input text can be passed for prompt)
dialog.dismiss() Dismiss dialog
dialog.message() Get the dialog message
dialog.type() Get the dialog type:'alert' | 'confirm' | 'prompt' | 'beforeunload'

New windows and new tabs

When clicking a link or button opens a new tab, Playwright provides several ways to obtain the new page.

page.waitForEvent('popup') to wait for popups

Example

test

(

'Handle new tabs'

,

async

(

{

page

}

)

=>

{

 

// Set up waiting for the new page event before clicking

 

const

popupPromise

=

page.

waitForEvent

(

'popup'

)

;

 

// Click the link that opens a new window

  await page.

getByRole

(

'link'

,

{

name

:

'New window opened'

}

)

.

click

(

)

;

 

// Get the newly opened page

 

const

popup

=

await popupPromise

;

 

// Operate on the new page

  await expect

(

popup

)

.

toHaveTitle

(

/RUNOOB/

)

;

  await popup.

getByRole

(

'button'

,

{

name

:

'OK'

}

)

.

click

(

)

;

 

// Close the new page

  await popup.

close

(

)

;

}

)

;

context.on('page') to listen for new pages

Example

test

(

'Listen for all new pages in the Context'

,

async

(

{

context

,

page

}

)

=>

{

 

// Listen for new page creation across the entire Context

  context.

on

(

'page'

,

async newPage

=>

{

    console.

log

(

'A new page opened:'

,

newPage.

url

(

)

)

;

   

// Wait for the new page to finish loading

    await newPage.

waitForLoadState

(

)

;

   

// Operate on the new page

 

}

)

;

  await page.

goto

(

'https://www.runoob.com/'

)

;

 

// Subsequent actions that open new windows will automatically trigger the above listener

}

)

;


Playwright event system

Playwright's Page and Context objects are event emitters, allowing you to listen for various events.

Common page events

Events Trigger Timing
request When the page makes a network request
response When a network response is received
dialog When an alert/confirm/prompt dialog appears
download When a file download starts
popup When a new page (popup/new tab) opens
console When the page outputs a console message
pageerror When an uncaught JavaScript error occurs on the page
load When the page finishes loading
close When the page is closed

Event listening methods

Example

// Listen continuously

page.

on

(

'request'

,

request

=>

{

  console.

log

(

'Request:'

,

request.

method

(

)

,

request.

url

(

)

)

;

}

)

;

// One-time listener (automatically removed after triggering once)

page.

once

(

'dialog'

,

dialog

=>

{

  dialog.

accept

(

)

;

}

)

;

// Remove listener

const

handler

=

(

request

)

=>

{

/* ... */

}

;

page.

on

(

'request'

,

handler

)

;

page.

off

(

'request'

,

handler

)

;