木偶|在iframe中输入文字

时间:2019-11-03 07:16:46

标签: javascript node.js iframe puppeteer headless

操纵者iframe输入问题

  • 我正在尝试从网站传递结帐表格,但是该表格具有用于卡片信息的iframe,有人知道如何以这种形式输入文本吗?

  • 我尝试了在堆栈溢出和GitHub上看到的所有内容,但是我看到的所有内容都无法帮助我。

  • 我没有尝试过的代码,因为尝试了很多之后都删除了。


期望填写卡号和持卡人姓名。

1 个答案:

答案 0 :(得分:0)

因此,当您尝试获取框架对象时,应侦听页面中的frameattached事件。您可以将此侦听器作为一个函数。

function waitForFrame(page, frameName) {
  let fulfill;
  const promise = new Promise(x => fulfill = x);
  checkFrame();
  return promise;

  function checkFrame() {
    const frame = page.frames().find(f => f.name() === frameName);
    if (frame)
      fulfill(frame);
    else
      page.once('frameattached', checkFrame);
  }
}

下面这些行是获取框架对象的方式

// 1. waiting for frame with name 'ifrw' to get attached
const frame = await waitForFrame(page, 'ifrw')

// 2. waiting for the frame to contain the necessary selector
await frame.waitForSelector('input[type="text"][name="username"]')

// 3. do whatever you want to do like login form for example
const nameInputHandle = await frame.$('input[type="text"][name="username"]')
await nameInputHandle.type('my_name_is_too_long_for_sure')

const passInputHandle = await frame.$('input[type="password"][name="password"]')
await passInputHandle.type('Even_the_password_is_tricky_12345')

const submitButton = await frame.$('input[type="submit"][name="loginsubmit"]')
await submitButton.click()