More Elegant Solution To Fetching a Selector From Within Two IFrames

Viewed 19

I've been working on a webscraper that uses puppeteer. The scraper, as shown below, aims to click on a button that is inside of two iframes, although the website that I'm working with generates the content of the iframes with a bunch of javascript and it can take upwards of 8 seconds for the button to render(during which there are also a number of open connections between server and client, so page.waitForNetworkIdle() is out of the question). The code here actually works; however, I'm sure of the existence of some significantly more elegant solution to this problem, most likely not involving async functions. I tried using a couple of promise-chaining patterns earlier but nothing seemed to work. If anyone could point me to some method for solving this problem with less jank, I'd be grateful.

  const t = 5000;
  function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
  //--------------------------------------------------//
  var tFrame = await page.waitForFrame(async fr => {
    return fr.name().length === 5
  })
  var ebRealModal = tFrame.childFrames().find(fra => {return fra.name() === 'ebRealModal'}); 
   var search = null
   while(search === null || search === undefined){
   tFrame = page.frames().find(fra => {return fra.name().length === 5});
   if(tFrame !== null && tFrame !== undefined){
   ebRealModal = tFrame.childFrames().find(fra => {return fra.name() === 'ebRealModal'});
   if(ebRealModal !== null && ebRealModal !== undefined){
     search = await ebRealModal.$("#search")
   }
  }
   await delay(t)
  }
  button = await ebRealModal.$("#search");
  await button.evaluate(b => b.click());
0 Answers
Related