木偶演员:找不到Javascript加载的元素

木偶演员:找不到Javascript加载的元素,javascript,jquery,puppeteer,Javascript,Jquery,Puppeteer,我一直在使用Puppeter来抓取一些网站,当我需要的元素在DOM中时,它工作得很好;但是,当元素通过Javascript加载时,我无法让它工作。请看下面我的代码。更具体地说,page.waitForSelector总是触发超时错误。我尝试了一个page.screenshot,结果图像显示了一个完全加载的页面,其中包含这个.evTextFont元素 如何修改此代码以成功检索.evTextFont元素 我已经尝试了木偶演员版本1.11和1.17,但我得到了同样的问题 非常感谢 改编自 const

我一直在使用Puppeter来抓取一些网站,当我需要的元素在DOM中时,它工作得很好;但是,当元素通过Javascript加载时,我无法让它工作。请看下面我的代码。更具体地说,page.waitForSelector总是触发超时错误。我尝试了一个page.screenshot,结果图像显示了一个完全加载的页面,其中包含这个.evTextFont元素

如何修改此代码以成功检索.evTextFont元素

我已经尝试了木偶演员版本1.11和1.17,但我得到了同样的问题

非常感谢

改编自

const puppeter=require('puppeter');
常量URL=https://www.paintbar.com.au/events-1/moments-in-moonlight';
启动({headless:true,args:['-no sandbox','-disable setuid sandbox']}){
const page=wait browser.newPage();
等待page.setViewport({宽度:1200,高度:600})
wait page.goto(URL,{waitUntil:'networkidle0'});
wait page.waitForSelector('.evTextFont');
等待page.addScriptTag({url:'https://code.jquery.com/jquery-3.2.1.min.js'});
//等待page.screenshot({path:'./image.jpg',键入:'jpeg'});
const result=wait page.evaluate(()=>{
试一试{
var数据=[];
$('.evTextFont')。每个(函数(){
const title=$(this.text();
数据推送({
“标题”:标题,
});
});
返回数据;
}捕捉(错误){
log(err.toString());
}
});
等待浏览器关闭();
对于(变量i=0;i
之所以发生这种情况,是因为您要查找的事件显示在另一个站点的
iframe
元素中,因此您需要首先找到该iframe,然后对其执行操作

await page.goto(URL, {waitUntil: 'networkidle0'});

// Looking for the iframe with the event
const frame = (await page.frames()).find(f => f.url().includes("events.wix.com"));

// Then do work as before, but on that frame
await frame.waitForSelector('.evTextFont');
await frame.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'});

const result = await frame.evaluate(() => {...})

谢谢@Vaviloff!这非常有效,我学到了一些新东西
await page.goto(URL, {waitUntil: 'networkidle0'});

// Looking for the iframe with the event
const frame = (await page.frames()).find(f => f.url().includes("events.wix.com"));

// Then do work as before, but on that frame
await frame.waitForSelector('.evTextFont');
await frame.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'});

const result = await frame.evaluate(() => {...})