1. 程式人生 > >win10 puppeteer安裝,採坑

win10 puppeteer安裝,採坑

環境:win10+nodev8.11.3++npm5.6.0   (python3.6)   不是相似環境的,直接離開,別浪費時間。

手上用的框架是面向原生APP設計的,作自動化時,遇到微信小程式控制元件資訊難以定位的問題,網上介紹puppeteer可以處理該問題,於是安裝來驗證一下可行性,先安裝踩一下坑,以及解決辦法,後面再進行驗證。

問題1:提示更新npm,那麼久更新吧!沒什麼好說的。

C:\Users\X>npm i puppeteer
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\X\package.json'
npm WARN X No description
npm WARN X No repository field.
npm WARN X No README data
npm WARN X No license field.

npm ERR! Cannot read property '0' of undefined


   ╭─────────────────────────────────────╮
   │                                     │
   │   Update available 5.6.0 → 6.4.0    │
   │       Run npm i npm to update       │
   │                                     │
   ╰─────────────────────────────────────╯


npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\X\AppData\Roaming\npm-cache\_logs\2018-08-26T09_54_48_048Z-debug.log

問題2:npm無法更新

C:\Users\X>npm i npm
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\X\package.json'
npm WARN X No description
npm WARN X No repository field.
npm WARN X No README data
npm WARN X No license field.

npm ERR! Cannot read property '0' of undefined

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\X\AppData\Roaming\npm-cache\_logs\2018-08-26T09_55_34_149Z-debug.log

那麼就用: cnpm install npm -g  解決 ,附上cnpm安裝方法: 

 

npm install -g cnpm --registry=https://registry.npm.taobao.org

 問題3:更新npm後依然不能安裝puppeteer:

C:\Users\X>npm i puppeteer
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\X\package.json'
npm WARN X No description
npm WARN X No repository field.
npm WARN X No README data
npm WARN X No license field.

npm ERR! Cannot read property '0' of undefined

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\X\AppData\Roaming\npm-cache\_logs\2018-08-26T10_03_08_555Z-debug.log

查了一圈,最簡單解決辦法: cnpm i puppeteer  (別問為什麼,其他我都試過了)

問題4:無法下載Chromium

ERROR: Failed to download Chromium r515411! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.

只要在處理問題3時,使用我推薦的方法就沒有問題4了。

C:\Users\X>cnpm i puppeteer
√ Installed 1 packages
√ Linked 39 latest versions
Downloading Chromium r579032 - 127.7 Mb [====================] 100% 0.0s
Chromium downloaded to C:\Users\X\node_modules\[email protected]@puppeteer\.local-chromium\win64-579032
√ Run 1 scripts
√ All packages installed (41 packages installed from npm registry, used 17s(network 1s), speed 235.89kB/s, json 40(297.7kB), tarball 0B)

 好吧,裝上了,那肯定要跑一下demo:

const puppeteer = require('puppeteer');
 
(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://www.baidu.com/');
  await page.screenshot({path: 'example.png'});
 
  await browser.close();
})();

demo有了,那麼怎麼跑呢?nodejs我是新手:將上面的程式碼儲存a.js,開啟cmd,鍵入node a.js,與a.js相同目錄下出現example.png,表示ok啦,其他參照 https://www.npmjs.com/package/puppeteer

沒有圖形介面,有時不知道它幹了什麼,可以加上引數:{headless: false} 這些上面的連線都有,在這貼出來,主要是加上後報錯了;

const puppeteer = require('puppeteer');
 
(async () => {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
  await page.pdf({path: 'hn.pdf', format: 'A4'});
 
  await browser.close();
})();

報錯:

C:\Users\X\Desktop\ui>node html_to_pdf.js
(node:600) UnhandledPromiseRejectionWarning: Error: Protocol error (Page.printToPDF): PrintToPDF is not implemented
    at Promise (C:\Users\X\node_modules\[email protected]@puppeteer\lib\Connection.js:202:56)
    at new Promise (<anonymous>)
    at CDPSession.send (C:\Users\X\node_modules\[email protected]@puppeteer\lib\Connection.js:201:12)
    at Page.pdf (C:\Users\X\node_modules\[email protected]@puppeteer\lib\Page.js:897:39)
    at C:\Users\X\Desktop\ui\html_to_pdf.js:7:14
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:600) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:600) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

 資訊太少,沒辦法定位問題,修改一下,將報錯的具體內容列印一下:

const puppeteer = require('puppeteer');
 
(async () => {
  const browser = await puppeteer.launch({headless: false, slowMo: 250});
  const page = await browser.newPage();
  await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
  try{
	  await page.pdf({path: 'hn.pdf', format: 'A4'});
  }catch(e){
	  console.log(e);
  }
  await browser.close();
})();

檢視報錯資訊:

C:\Users\X\Desktop\ui>node html_to_pdf.js
{ Error: Protocol error (Page.printToPDF): PrintToPDF is not implemented
    at Promise (C:\Users\X\node_modules\[email protected]@puppeteer\lib\Connection.js:202:56)
    at new Promise (<anonymous>)
    at CDPSession.send (C:\Users\X\node_modules\[email protected]@puppeteer\lib\Connection.js:201:12)
    at Page.pdf (C:\Users\X\node_modules\[email protected]@puppeteer\lib\Page.js:897:39)
    at C:\Users\X\Desktop\ui\html_to_pdf.js:8:15
    at <anonymous>
  message: 'Protocol error (Page.printToPDF): PrintToPDF is not implemented' }

明顯在win10下PrintToPDF執行錯誤,查了一圈,暫不糾結,先跳過,後面處理。

npmc安裝的chromium地址:C:\Users\X\node_modules\puppeteer\.local-chromium\win64-579032\chrome-win32 (X為使用者名稱),若想更換,或使用指定版本的chromium,可以根據需求在指令碼中修改。