2017-12-18 8 views
0

私はページ上のすべてのノードのスタイルをすべて取得しようとしていますが、CSS.getMatchedStylesForNodedevtool-protocolから使いたいですが、そのノードは1つのノードでしか動作しません。ノードの配列をループすると、コンソール(以下のコード)で多くの警告が表示され、何も返されません。私は間違っているの?puppeteerでdevtool-protocolですべてのスタイルを取得する

コンソールに警告:

(node:5724) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 11): Error: Protocol error (CSS.getMatchedStylesForNode): Target closed. 

自分のコード

'use strict'; 

const puppeteer = require('puppeteer'); 

(async() => { 
    const browser = await puppeteer.launch(); 
    const page = await browser.newPage(); 
    await page.goto('https://example.com'); 
    await page._client.send('DOM.enable'); 
    await page._client.send('CSS.enable'); 
    const doc = await page._client.send('DOM.getDocument'); 
    const nodes = await page._client.send('DOM.querySelectorAll', { 
     nodeId: doc.root.nodeId, 
     selector: '*' 
    }); 

    const styleForSingleNode = await page._client.send('CSS.getMatchedStylesForNode', {nodeId: 3}); 
    const stylesForNodes = nodes.nodeIds.map(async (id) => { 
     return await page._client.send('CSS.getMatchedStylesForNode', {nodeId: id}); 
    }); 

    console.log(JSON.stringify(stylesForNodes)); 
    console.log(JSON.stringify(styleForSingleNode)); 

    await browser.close(); 
})(); 
  • 人形遣いバージョン:0.13.0
  • プラットフォーム:ウィンドウ10
  • ノード:8.9.3
+0

順番に配列を反復してみてください。現在、すべてのノードの接続を同時に開きます。 – wOxxOm

答えて

1

forループを使用して動作する

const stylesForNodes = [] 
for (id of nodes.nodeIds) { 
    stylesForNodes.push(await page._client.send('CSS.getMatchedStylesForNode', {nodeId: id})); 
} 
+0

ありがとう、これは私の問題を解決した:D –

関連する問題