编辑:我解决了我的问题。我找到了答案here。
我正在尝试使用Puppeteer,node.js和jquery创建一个自动化程序。
我想转到reddit上的页面并计算具有' s1okktje-0 '类的h2标签的数量。每当我运行该程序时,即使它应该输出25,它也始终输出0。为什么它不起作用?
const puppeteer = require('puppeteer');
async function count () {
const browser = await puppeteer.launch({
headless: false,
args:
[
'--disable-notifications',
]
});
function delaySeconds(timeout) {
return new Promise((resolve) => {
setTimeout(() => { resolve(timeout) }, timeout * 1000);
});
}
const page = await browser.newPage();
await page.goto('https://www.reddit.com/user/warnakey/', {waitUntil: 'networkidle2'});
await page.waitForSelector('button.pqvesn-2.kOkfJW');
await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'})
await delaySeconds(15);
let numItems = (await page.$$('h2.s1okktje-0')).length;
console.log("The number of items with class 's1okktje-0' is " + numItems);
console.log("The number above should be 25");
await browser.close()
}
count()