2017-12-18 6 views
0

페이지의 모든 노드에 대해 모든 스타일을 얻으려는 중이며 CSS.getMatchedStylesForNodedevtool-protocol에서 사용하고 싶지만 하나의 노드에서만 작동합니다. 루프 노드의 배열을 통해 나는 콘솔 (아래 코드)에서 많은 경고를 얻을 아무것도 반환됩니다. 내가 뭘 잘못하고있어?괴짜의 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 조 –