这一切都有效。它加载第一页并创建一个循环来打开列表中的每个链接。我遇到的问题是 const name = await page.evaluate(() => document.querySelector('li.inline.t-24.t-black.t-normal.break-words').innerText);
。我不明白为什么它引用第一个加载的页面而不是使用 link.click();
打开的页面。有人可以解释一下吗?
(async function main() {
try {
// launch puppeteer
const browser = await puppeteer.launch({headless: false});
// open browser
const page = await browser.newPage();
page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3844.0 Safari/537.36');
// load first page to generate list of links to scrape
await page.goto("https://www.websitepage1.come");
// wait for page to load
await page.waitForSelector('h4 > span')
// get list of buttons on page
const lists = await page.$$('ul.search-results__list.list-style-none > li.search-result.search-result__occluded-item.ember-view');
// loop through list of links
for (let i = 0; i < lists.length; i++) {
const list = lists[i]
const link = await list.$('a.search-result__result-link.ember-view');
const linkName = await list.$('a.search-result__result-link.ember-view span.name.actor-name');
const linkeNameText = await page.evaluate(linkName => linkName.innerText, linkName)
// open the link
link.click();
await page.waitForSelector('h4 > span')
await page.waitForSelector('li.inline.t-24.t-black.t-normal.break-words')
// THIS IS WHERE THE ERROR IS OCCURING. IT RETURNS AN ELEMENT FROMawait page.goto("https://www.websitepage1.come");
const name = await page.evaluate(() => document.querySelector('li.inline.t-24.t-black.t-normal.break-words').innerText);
console.log('name', name);
console.log('\n\n');
}
} catch (e) {
console.log('our error', e);
}
})();
最佳答案
单击该链接后,您需要等待新页面加载,否则脚本的其余部分将使用第一个加载的页面。您需要等待navigation
:
// open and process the first page
await Promise.all([
page.waitForNavigation(), // The promise resolves after navigation has finished
link.click(), // Clicking the link will indirectly cause a navigation
]);
// now process the second page
关于javascript - 循环访问页面链接 puppeteer 不会从新加载的页面返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57532897/