Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Puppeteer 木偶演员-如何填充iframe中的表单?_Puppeteer - Fatal编程技术网

Puppeteer 木偶演员-如何填充iframe中的表单?

Puppeteer 木偶演员-如何填充iframe中的表单?,puppeteer,Puppeteer,我必须填写一张在iframe中的表格,这里是。我不能简单地使用page.focus()和page.type()访问。我试图通过使用const formFrame=page.mainFrame().childFrames()[0]来获取表单iframe,这很有效,但我无法真正与表单iframe交互。我不想弄清楚如何进入iframe并键入,而是直接导航到iframe URL来简化问题 让您的脚本直接转到上面的URL并尝试自动化,它应该可以工作 编辑-1:使用框架 因为简单的方法不适用于您,所以我们

我必须填写一张在iframe中的表格,这里是。我不能简单地使用
page.focus()
page.type()
访问。我试图通过使用
const formFrame=page.mainFrame().childFrames()[0]
来获取表单iframe,这很有效,但我无法真正与表单iframe交互。

我不想弄清楚如何进入iframe并键入,而是直接导航到iframe URL来简化问题

让您的脚本直接转到上面的URL并尝试自动化,它应该可以工作

编辑-1:使用框架

因为简单的方法不适用于您,所以我们使用框架本身

下面是一个简单的脚本,可以帮助您开始

const puppeter=require('puppeter');
(异步()=>{
const browser=wait puppeter.launch({headless:false});
const page=wait browser.newPage();
等待页面。转到('http://www.goodmanmfg.com/product-registration“,{超时:80000});
var frames=wait page.frames();
var myframe=frames.find(
f=>
f、 url().indexOf(“新注册”)>-1);
const serialNumber=wait myframe.$(“#main content_SerNumText”);
等待serialNumber.type(“12345”);
等待page.screenshot({path:'example.png'});
等待浏览器关闭();
})();
输出是


我自己想出来的。这是密码

console.log('等待带有表单的iframe准备就绪');
wait page.waitForSelector('iframe');
log('iframe已就绪。正在加载iframe内容');
const elementHandle=等待页面$(
'iframe[src='https://example.com"]',
);
const frame=await elementHandle.contentFrame();
console.log(“在iframe中填写表单”);
wait frame.type('#Name',Bob',{delay:100});

虽然你已经明白了,但我认为我有更好的解决方案。希望能有帮助

async doFillForm() {
    return await this.page.evaluate(() => {
       let iframe = document.getElementById('frame_id_where_form_is _present');
       let doc = iframe.contentDocument;
       doc.querySelector('#username').value='Bob';
       doc.querySelector('#password').value='pass123';
    });
}

如果无法选择/查找iFrame,请阅读以下内容:

我在查找条纹元素时遇到问题。 原因如下:

您不能使用JavaScript访问具有不同来源的文件,如果您可以这样做,这将是一个巨大的安全漏洞。对于同源策略浏览器,阻止脚本尝试访问具有不同来源的帧

因此,当我尝试使用Puppeter的方法时:
Page.frames()
Page.mainFrame()。
ElementHandle.contentFrame()
我没有返回任何iframe给我。问题是它一直在悄无声息地发生,我不明白为什么它找不到任何东西

将这些参数添加到启动选项解决了问题:

“--禁用web安全性”,
“--禁用功能=隔离源,每个进程的站点”

什么样的网页外观,请考虑另一个不能帮助你,如果我们看不到问题。这是网站。我想自动填写这个网站,并自动注册许多序列号。该页面太重,大约6.6MB。你用的是什么木偶版本?我用的是现在可以买到的版本。0.13.0我无法继续,因为我没有有效的序列号。我也尝试了。但在下一页中,您将与iframe和主框架进行交互。这里“延迟”似乎很重要。否则它会输入随机的东西。不确定这是错误还是什么。
$
是多余的
waitForSelector
已返回元素句柄。