How to get console.log output in Terminal via Headless Chrome Runtime.evaluate

前端 未结 2 1781
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 05:07

I am following this issue post here:

https://github.com/cyrus-and/chrome-remote-interface/issues/105

But I cannot seem to get console.log output

2条回答
  •  伪装坚强ぢ
    2021-01-02 05:56

    For those who want a less complicated way to get data from a page I'd recommend puppeteer. It has a much clearer higher-level API than chrome-remote-interface. The best feature in my opinion is its ability to accept javascript functions instead of strings for evaluation.

    Let's say we want to fetch some data from API in context of a given page.

    const puppeteer = require('puppeteer');
    
    (async () => {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();
      await page.goto('https://example.com/');
    
      let result = await page.evaluate(async () => {
        // here comes the code which gets executed in browser
        return await fetch('index.html').then(response => response.text());
      });
    
      console.log(result);
    
      await browser.close();
    })();
    

提交回复
热议问题