2017-11-26 4 views

答えて

1

からの簡単な再現性の例として

、こすり、This is a paragraph、あなたがのコンテキストでJavaScriptを評価するために、人形遣いのpage.evaluateを使用することができますiframeの内容を返すページ。そうする

手順は次のとおりです。

  1. グラブiframe要素
  2. iframedocumentオブジェクトを取得します。
  3. 私はlink you providedからThis is a paragraphをつかみ、このプログラムを書いたiframeのHTML

を読むためにdocumentオブジェクトを使用します。

const puppeteer = require("puppeteer"); 

(async() => { 

    const browser = await puppeteer.launch(); 

    const page = await browser.newPage(); 
    await page.goto('https://www.w3schools.com/js/tryit.asp?filename=tryjs_events'); 

    const iframeParagraph = await page.evaluate(() => { 

     const iframe = document.getElementById("iframeResult"); 

     // grab iframe's document object 
     const iframeDoc = iframe.contentDocument || iframe.contentWindow.document; 

     const iframeP = iframeDoc.getElementById("demo"); 

     return iframeP.innerHTML; 
    }); 

    console.log(iframeParagraph); // prints "This is a paragraph" 

    await browser.close(); 

})(); 
関連する問題