jQuery 在 Puppeteer 中等效于 find().filter()
jQuery find().filter() equivalent in Puppeteer
我在 jQuery 中有此代码:
$(iframe).find('input[type=text]').filter(':visible:first').val("abc");
我试着在 Puppeteer 中这样做:
const iframe = await page.frames().find(f => f.name() === 'iframe');
var inputText = await iframe.$('input[type=text]').filter(':visible:first');
await inputText.focus();
await page.keyboard.type("abc");
我收到以下错误:
iframe.$(...).filter is not a function
我也尝试过使用 iframe。$$ 但得到了相同的结果。
更新:
我的最终目标是重现这个jQuery:
$(iframe).find('select').filter(':visible').eq(1);
jQuery 上的 $ 和 puppeteer 上的 $ 没有关系。您需要在页面上注入 jQuery 和评估脚本。
从cdn注入jQuery,
await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.4.1.min.js'});
注入本地jQuery,
await page.addScriptTag({path: require.resolve('jquery')});
然后您的代码通常使用 page.evaluate
、
await page.evaluate(()=>{
// get the body inside iframe
const iframe = $("YourSelector").contents().find("body");
// run your code
$(iframe).find('input[type=text]').filter(':visible:first').val("abc");
})
我在 jQuery 中有此代码:
$(iframe).find('input[type=text]').filter(':visible:first').val("abc");
我试着在 Puppeteer 中这样做:
const iframe = await page.frames().find(f => f.name() === 'iframe');
var inputText = await iframe.$('input[type=text]').filter(':visible:first');
await inputText.focus();
await page.keyboard.type("abc");
我收到以下错误:
iframe.$(...).filter is not a function
我也尝试过使用 iframe。$$ 但得到了相同的结果。
更新:
我的最终目标是重现这个jQuery:
$(iframe).find('select').filter(':visible').eq(1);
jQuery 上的 $ 和 puppeteer 上的 $ 没有关系。您需要在页面上注入 jQuery 和评估脚本。
从cdn注入jQuery,
await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.4.1.min.js'});
注入本地jQuery,
await page.addScriptTag({path: require.resolve('jquery')});
然后您的代码通常使用 page.evaluate
、
await page.evaluate(()=>{
// get the body inside iframe
const iframe = $("YourSelector").contents().find("body");
// run your code
$(iframe).find('input[type=text]').filter(':visible:first').val("abc");
})