Handle Nested Frames in Playwright Java

Profile picture for user arilio666

In Playwright, handling nested frames refers to the ability to access and interact with frames within a webpage that are nested within other frames. Nested frames are contained within different frames rather than directly within the webpage's main frame.

This article will go through handling nested frames using playwright java.

  • This is a herokuapp site that has nested frames.
  • In this, we can see that frames are present within the frameset.
  • Let us see how we can make our way to the child frame.

To gain entry inside the three frames on top, we need to first break inside this frame-top which is the parent.
The parent covers the whole child.

        FrameLocator parentFrame = page.frameLocator("//frame[@name='frame-top']");

Using the frameLocator, we located the parent frame.

        FrameLocator middleFrame = parentFrame.frameLocator("//frame[@name='frame-middle']");

Using this parent frame, we can now access the frame we want we ease.

        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/nested_frames");
        FrameLocator parentFrame = page.frameLocator("//frame[@name='frame-top']");
        FrameLocator middleFrame = parentFrame.frameLocator("//frame[@name='frame-middle']");
        String textContent = middleFrame.locator("body").textContent();
        System.out.println(textContent);
        

This is how we can handle nested frames in playwright java.