我在jQuery中具有此代码:
$(iframe).find('input[type=text]').filter(':visible:first').val("abc");
我试图在木偶中这样做:
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");
})