Handle Frame in Playwright Java

Profile picture for user arilio666

The playwright provides several methods to handle and interact with frame or iframe elements.  This article will discuss handling frames when we encounter one using playwright java.

Frames are a shell for a webpage that contains contents within this shell. To access its content, we need to breach the outer shell.

  • Take this site, for example. Let us try to breach this middle Frame and enter a name and type some content inside of "Enter Your Suggestions."
  Playwright playwright = Playwright.create();
        Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
        Page page = browser.newPage();
        page.navigate("http://www.maths.surrey.ac.uk/explore/nigelspages/frame2.htm");

        FrameLocator middleFrame = page.frameLocator("//frame[@src='message.htm']");

        middleFrame.locator("//input[@name='name']").type("Naruto Uzumaki");
        middleFrame.locator("//textarea[@name='suggestions']").type("I Am Inside The Frame");      
  • Using the frameLocator, we accessed the middle Frame by passing its xpath locator.
  • This enabled us to penetrate the Frame and do modifications inside it.

There are other ways of identifying frames using Frame, and we can also use a framelocator as we used here:

Get Frame using Name FrameSelector.

page.frame("testframe")

Get Frame using Name Option.

page.frame({name: "testframe"})

Get Frame using the URL option

page.frame({url: "http://autopract.com/selenium/form1/"})

Get Frame using Partial URL

page.frame({url: /form1/})