Search code examples
iframeplaywrightplaywright-test

Retrieving iFrame content using Playwright


I am trying the contents of iFrame and the iFrame doesn't have an src or URL. It has id element. Below is code I am trying to use . Is there anything I am missing here ? Content is empty

await page.goto("https://sites.google.com/view/pinnednote/home", { waitUntil: 'load', timeout: 30000 });
const myFrames = await page.frames();
console.log("Parent IFrame = "+myFrames.length)

for ( x of myFrames) { // Getting all iFrames
  try {
    const frameElement = await x.frameElement();
    const contentFrame = await frameElement.contentFrame();
    console.log(await contentFrame.content());
  } catch(error){
    console.log(error)
  }
  childs = await x.childFrames();
  console.log("Child IFrame = "+childs.length)
  for ( y of childs) { 
    const frameElement = await y.frameElement();
    const contentFrame = await frameElement.contentFrame();
    console.log(await contentFrame.content());
  }
}

Solution

  • Try this:

    await page.goto("https://sites.google.com/view/pinnednote/home", { waitUntil: 'load', timeout: 30000 });
    const myFrames = await page.frames();
    console.log("Parent IFrame = "+myFrames.length)
    
    for ( x of myFrames) { // Getting all iFrames
      try {
        const frameContent = await x.content();
        console.log(frameContent)
      } catch(error){
        console.log(error)
      }
    }
    

    You already have the frames in your myFrames array. When you are making the loop you only need to take the content.