首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >.wait() nightmare和Puppeteer计算都找不到ID

.wait() nightmare和Puppeteer计算都找不到ID
EN

Stack Overflow用户
提问于 2019-04-22 16:15:29
回答 1查看 408关注 0票数 3

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

因此,从技术上讲,如果您转到该链接并单击反馈点击,它将显示所有评论

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

下面是代码

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()

我该怎么办?

EN

回答 1

Stack Overflow用户

发布于 2019-04-25 03:32:32

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();

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55791269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档