[英]Puppeteer - why is innerText not printed on console while every element does?
[英]Why Does My Puppeteer .innerText code return nothing?
所以我剛開始學習 puppeteer.js,發生的事情是我的代碼運行它沒有錯誤,但它沒有向控制台顯示任何內容。 (我將 Node.js 用於調試目的)唯一的顯示是當我將異步 function 放入另一個 function 時,即使它返回未定義。 我想知道為什么會這樣以及如何解決它。
這是我的代碼:
(async () =>{
let movieUrl = 'https://www.imdb.com/title/tt0111161/?ref_=nav_sr_1'
let browser = await puppeteer.launch();
let page = await browser.newPage();
await page.goto(movieUrl, {waitUntil: 'networkidle2'})
let data = await page.evaluate(() => {
let title = document.querySelector('div[class="title_wrapper"] > h1').innerText;
let rating = document.querySelector('span[itemprop="ratingValue"]').innerText;
let ratingCount = document.querySelector('span[itemprop="ratingCount"]').innerText;
return{title, rating, ratingCount};
})
console.log(data);
debugger;
await browser.close();
})
這是一個未命名的異步 function:
async () => {
// the function code
}
如果你想立即運行它,你需要調用它。 您可以通過將其括在括號中來做到這一點:
(async () => {
// the function code
})()
這稱為 IIFE,立即調用 Function 表達式。 您聲明一個 function 並立即運行它。
請參閱這些文章以了解它:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.