In this blog we will be utilizing Playwright Java to handle frames.
Topics that we will cover:
- Fundamental overview of frames
- Handle frames
- Source code
Fundamental overview of frames
A frame refers to an HTML page that is inserted into another page.
Launch https://paytm.com/ and hit ‘Sign In’ button

A distinct window pops up (as seen above) and this is a frame. The question arises as to how do we know if this popup is indeed a frame?
By clicking the right mouse button on this window, the option to view the frame source becomes visible

Clicking outside the window will not show the 'View frame source' option (see below). This means that this part of the html page is not a frame

This explanation provided a fundamental overview of frames.
Handle frames
We will now automate an action on an iframe. Navigate to

Let's examine the procedure for automating a test that will input text into the text area located in the frame above.
The text area is represented by tag ‘iframe’ and has the ‘id’ mce_0_ifr

Since this frame is within a parent html page, the locator of parent html can than be written as shown below

This is how our code looks

Execute.
The text appears in the text area of the frame as shown below

Playwright does not require switching to a frame, unlike Selenium.
Source code
package com.rsa.playwrightjava;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class Blog27_iframes_PWJava_Example1 {
public static void main(String[] args) {
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
Page page = browser.newPage();
page.navigate("https://the-internet.herokuapp.com/iframe");
Locator frame1 = page.frameLocator("#mce_0_ifr").locator("html");
frame1.click();
frame1.type("https://rahulshettyacademy.com");
page.pause();
}
}
Thanks