Categories
JavaScript Answers

How to submit a form with Node Puppeteer?

To submit a form with Node Puppeteer, we call click on the submit button.

For instance, we write

await page.goto("https://www.example.com/login");

await page.type("#username", "username");
await page.type("#password", "password");

await page.click("#submit");

await page.waitForNavigation();

to call goto to go to the page.

Then we call type to enter the values for the inputs with IDs username and password.

And then we call click to click the button with ID submit to submit the form.

Categories
JavaScript Answers

How to fix Cannot find module babel-preset-es2015 error with JavaScript?

To fix Cannot find module babel-preset-es2015 error with JavaScript, we install the @babel/preset-env package.

To install it, we run

npm install --save-dev @babel/preset-env

to install the package.

Then in .babelrc, we write

{
  "presets": ["@babel/preset-env"]
}

to add the preset.

Categories
JavaScript Answers

How to use the async lib – async.foreach with object with Node.js?

To use the async lib – async.foreach with object with Node.js, we call forEach with a callback for each item.

For instance, we write

async.forEach(
  Object.keys(dataObj),
  (item, callback) => {
    console.log(item);
    callback();
  },
  (err) => {
    console.log("iterating done");
  }
);

to call forEach with a callback with the callback parameter that we call in the callback.

The callback in the 2nd argument is called when the async process is done.

Categories
JavaScript Answers

How to salt and hash password in Node with crypto?

To salt and hash password in Node with crypto, we call the hashSync method.

For instance, we write

const bcrypt = require("bcrypt");
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync("B4c0//", salt);

to create a salt with genSaltSync.

Then we call hashSync with the string to create a hash for and the salt to create the hash.

We then compare the hash with

const isSame = bcrypt.compareSync("B4c0//", hash);

by calling compareSync with the unhashed string with the hash.

Categories
JavaScript Answers

How to delete existing text from input using Node Puppeteer?

To delete existing text from input using Node Puppeteer, we set the value of the input to an empty string.

For instance, we write

await page.evaluate(() => (document.getElementById("inputID").value = ""));

to call page.evaluate with a callback that gets the input with getElementById.

And we set the value property to an empty string to empty the input.