fill()

fill(selector, inputs [, submit, callback])

Fills a form with the given values and optionally submits it.

Inputs are referenced by their name attribute.

— selector (String)

CSS selector targeting what form to fill. It should point to a form tag. Make sure the target form is visible or present by calling waitUntilVisible() or waitUntilPresent() beforehand.

— inputs (PlainObject)

An object containing the data you want to enter in the form.
Keys must correspond to the inputs' name attribute. This method supports single select fields in the same way as normal input fields. For select fields allowing multiple selections, supply an array of values to match against.

— options (Boolean)

  • submit (Boolean): Whether or not to submit the form after filling it (false by default).

— callback (Function(err))

Function called when finished.

  • err (String): null or a string describing what went wrong when filling the form
const selector = "#contact-form"
const inputs = {
  "subject": "I am watching you",
  "content": "So be careful.",
  "civility": "Mr",
  "name": "Chuck Norris",
  "email": "[email protected]",
  "cc": true,
  "attachment": "roundhousekick.doc" // file taken from your agent's disk
}

try {
  await tab.waitUntilVisible(selector, 5000)
  await tab.fill(selector, inputs, { submit: true })
  console.log("Form sent!")
  // Continue your navigation in this branch
  // You should probably do a waitUntilVisible() or waitUntilPresent() here
} catch (err) {
  console.log("Form not found:", err)
}
const selector = "#contact-form"
const inputs = {
  "subject": "I am watching you",
  "content": "So be careful.",
  "civility": "Mr",
  "name": "Chuck Norris",
  "email": "[email protected]",
  "cc": true,
  "attachment": "roundhousekick.doc" // file taken from your agent's disk
}

tab.waitUntilVisible(selector, 5000)
.then(() => {
  return tab.fill(selector, inputs, { submit: true })
})
.then(() => {
  console.log("Form sent!")
  // Continue your navigation in this branch
  // You should probably do a waitUntilVisible() or waitUntilPresent() here
})
.catch((err) => {
  console.log("Form not found:", err)
})
var selector = "#contact-form"
var inputs = {
  "subject": "I am watching you",
  "content": "So be careful.",
  "civility": "Mr",
  "name": "Chuck Norris",
  "email": "[email protected]",
  "cc": true,
  "attachment": "roundhousekick.doc" // file taken from your agent's disk
}

tab.waitUntilVisible(selector, 5000, function(err) {
  if (err) {
    console.log("Form not found:", err)
  } else {
    tab.fill(selector, inputs, { submit: true }, function(err) {
      if (err) {
        console.log("Could not fill the form:", err)
      } else {
      	console.log("Form sent!")
        // Continue your navigation in this branch
        // You should probably do a waitUntilVisible() or waitUntilPresent() here
      }
    })
  }
})
<form action="/contact" id="contact-form" enctype="multipart/form-data">
  <input type="text" name="subject"/>
  <textearea name="content"></textearea>
  <input type="radio" name="civility" value="Mr"/> Mr
  <input type="radio" name="civility" value="Mrs"/> Mrs
  <input type="text" name="name"/>
  <input type="email" name="email"/>
  <input type="file" name="attachment"/>
  <input type="checkbox" name="cc"/> Receive a copy
  <input type="submit"/>
</form>