How to run Puppeteer code in any web browser?

风流意气都作罢 提交于 2019-11-26 22:10:38

问题


I'm trying to do some web scraping with Puppeteer and I need to retrieve the value into a Website I'm building.

I have tried to load the Puppeteer file in the html file as if it was a JavaScript file but I keep getting an error. However, if I run it in a cmd window it works well.

Scraper.js:

getPrice();
function getPrice() {
    const puppeteer = require('puppeteer');
    void (async () => {
        try {
            const browser = await puppeteer.launch()
            const page = await browser.newPage()              
            await page.goto('http://example.com') 
            await page.setViewport({ width: 1920, height: 938 })        
            await page.waitForSelector('.m-hotel-info > .l-container > .l-header-section > .l-m-col-2 > .m-button')
            await page.click('.m-hotel-info > .l-container > .l-header-section > .l-m-col-2 > .m-button')
            await page.waitForSelector('.modal-content')
            await page.click('.tile-hsearch-hws > .m-search-tabs > #edit-search-panel > .l-em-reset > .m-field-wrap > .l-xs-col-4 > .analytics-click')
            await page.waitForNavigation();
            await page.waitForSelector('.tile-search-filter > .l-display-none')
            const innerText = await page.evaluate(() => document.querySelector('.tile-search-filter > .l-display-none').innerText);
            console.log(innerText)
        } catch (error) {
            console.log(error)
        }

    })()
}

index.html:

<html>
  <head></head>
  <body>
    <script src="../js/scraper.js" type="text/javascript"></script>
  </body>
</html>

The expected result should be this one in the console of Chrome:

But I'm getting this error instead:

Any ideas?

Thank you in advance!


回答1:


It does work with browser. The package is called puppeteer-web, specifically made for such cases.

But the main point is, there must be some instance of chrome running on some server. Only then you can connect to it.

To bundle Puppeteer using Browserify:

Clone Puppeteer repository:

git clone https://github.com/GoogleChrome/puppeteer && cd puppeteer
npm install
npm run bundle

This will create ./utils/browser/puppeteer-web.js file that contains Puppeteer bundle.

You can use it later on in your web page to drive another browser instance through its WS Endpoint:

<script src='./puppeteer-web.js'></script>
<script>
  const puppeteer = require('puppeteer');
  const browser = await puppeteer.connect({
    browserWSEndpoint: '<another-browser-ws-endpont>'
  });
  // ... drive automation ...
</script>

I had some fun with puppeteer and webpack,

  • playground-react-puppeteer
  • playground-electron-react-puppeteer-example

See these answers for full understanding of creating the server and more,

  • Official link to puppeteer-web
  • Puppeteer with docker
  • Puppeteer with chrome extension
  • Puppeteer with local wsEndpoint


来源:https://stackoverflow.com/questions/54647694/how-to-run-puppeteer-code-in-any-web-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!