Handle Multiple Frames in Playwright Java

Profile picture for user arilio666

In playwright java, a web page can contain multiple frames, essentially independent HTML documents nested within the main document.

The playwright provides several methods to handle and interact with frame or iframe elements.

  • Consider this site. For example, this has three frames.
  • These are the three frames within this page, and we will traverse between each frame and perform operations.
    We will be handling the top two frames in this article.
    page.navigate("http://www.maths.surrey.ac.uk/explore/nigelspages/frame2.htm");

        FrameLocator frameOne = page.frameLocator("//frame[@src='htmlf.htm']");
        Locator firstBox = frameOne.locator("//a[normalize-space()='Beginners Guide']");
        System.out.println(firstBox.textContent());
        firstBox.click();
        page.pause();
        FrameLocator frameTwo = page.frameLocator("//frame[@src='message.htm']");
        Locator secondBox = frameTwo.locator("//input[@name='name']");
        secondBox.type("Naruto");
  • With the page context, we can effectively traverse between multiple frames as we did here between the top two frames.
  • We traversed into each frame separately and performed some operations.
  • This is how we can handle multiple frames in playwright java.