gpt4 book ai didi

node.js - .wait() nightmare 和 Puppeteer evaluate 都找不到 ID

转载 作者:搜寻专家 更新时间:2023-10-31 23:50:04 24 4
gpt4 key购买 nike

我试图在速卖通中获取评论,但由于某些原因,wait() 函数总是找不到 #transction-feedback

所以从技术上讲,如果您转到该链接并点击“反馈”,它会显示所有评论

.click 确实有效,因为它单击了选项卡,但似乎 #transction-feedback 从未出现过。我尝试使用浏览器重新创建相同的内容,#transction-feedback 出现了。

enter image description here

这是代码

const cheerio = require('cheerio')
const Nightmare = require('nightmare')
const nightmare = Nightmare({
show: true
})


const URL = 'https://www.aliexpress.com/item/Samsung-Earphones-EHS64-Headsets-With-Built-in-Microphone-3-5mm-In-Ear-Wired-Earphone-For-Smartphones/32854052487.html?spm=2114.search0103.3.1.51dd26c3CxZ3zZ&ws_ab_test=searchweb0_0,searchweb201602_8_10065_10068_319_10059_10884_317_10887_10696_321_322_10084_453_10083_454_10103_10618_10307_537_536,searchweb201603_50,ppcSwitch_0&algo_expid=5587ff37-5e3a-45a8-8206-7b182433a961-0&algo_pvid=5587ff37-5e3a-45a8-8206-7b182433a961'

nightmare
.goto(URL)
.click('li[data-trigger="feedback"]')
.wait('#transction-feedback')
.evaluate(() => {
return document.body.innerHTML

})
.end()
.then((result) => {
const $ = cheerio.load(result)

res.json($.html())

})
.catch(error => {
console.error('Search failed:', error)
})

我也尝试过 Puppeteer,但没有用

const puppeteer = require('puppeteer');
const URL = 'https://www.aliexpress.com/item/Samsung-Earphones-EHS64-Headsets-With-Built-in-Microphone-3-5mm-In-Ear-Wired-Earphone-For-Smartphones/32854052487.html?spm=2114.search0103.3.1.51dd26c3CxZ3zZ&ws_ab_test=searchweb0_0,searchweb201602_8_10065_10068_319_10059_10884_317_10887_10696_321_322_10084_453_10083_454_10103_10618_10307_537_536,searchweb201603_50,ppcSwitch_0&algo_expid=5587ff37-5e3a-45a8-8206-7b182433a961-0&algo_pvid=5587ff37-5e3a-45a8-8206-7b182433a961'


async function testPupp() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(URL);
await page.evaluate(() => {
document.querySelector('li[data-trigger="feedback').click();

console.log(document.body.innerHTML)
});

await browser.close();
}


testPupp()

我该怎么办?

最佳答案

iframe 是问题所在:试试这段代码

const puppeteer = require("puppeteer");
const URL =
"https://www.aliexpress.com/item/Samsung-Earphones-EHS64-Headsets-With-Built-in-Microphone-3-5mm-In-Ear-Wired-Earphone-For-Smartphones/32854052487.html?spm=2114.search0103.3.1.51dd26c3CxZ3zZ&ws_ab_test=searchweb0_0,searchweb201602_8_10065_10068_319_10059_10884_317_10887_10696_321_322_10084_453_10083_454_10103_10618_10307_537_536,searchweb201603_50,ppcSwitch_0&algo_expid=5587ff37-5e3a-45a8-8206-7b182433a961-0&algo_pvid=5587ff37-5e3a-45a8-8206-7b182433a961";

async function testPupp() {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto(URL, { waitUntil: "networkidle2" });

// evade popup
await page.click("body");
await page.keyboard.press("Escape");

await page.click('li[data-trigger="feedback');

// wait for iframe to load
await page.waitFor(2000);

const frame = (await page.frames()).find(f => f.url().includes("feedback"));

await frame.waitFor("#transction-feedback");

await frame.waitFor(".feedback-item");
const els = await frame.$$(".feedback-item");
for (const el of els) {
const text = await (await el.getProperty("textContent")).jsonValue();
console.log(text.trim().replace(/\s\s+/g, " "));
}

await browser.close();
}

testPupp();

关于node.js - .wait() nightmare 和 Puppeteer evaluate 都找不到 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55791269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com