0

I'm super new to coding and building a script that navigates to a page and takes a screenshot of it.

So far, I've figured out just from googling, etc. how to login if the credentials are coded in puppeteer. However, I want for more than one person to be able to use this with their own credentials without having to go into the code and change it that way, so I decided to make a prompt.

I found some basic information on google and code that prompts for a user ID and password in the command line and put that into my code. It asks for a username and password and then just saves it.

I need help figuring out how to take the logged userID and password and have puppeteer input those into a website login. Eventually I'd like to generate a popup prompt that allows the user to input ID and password if possible. Any ideas?

Here's my code:

const fs = require("fs");
const puppeteer = require("puppeteer");

async function captureScreenshot() {
// creates a screenshot directory
if (!fs.existsSync("screenshots")) {
 fs.mkdirSync("screenshots");
}

let browser = null;

try {
  // launch headless Chromium browser
browser = await puppeteer.launch({ headless: true });

// new tab/page
const page = await browser.newPage();

//viewport width and height
await page.setViewport({ width: 1440, height: 1080 });

await page.goto("https://users.nexusmods.com/");

//Prompt for User ID/Password:
var prompt = require('prompt');
prompt.start();


prompt.get([{
 name: 'username',
 required: true
}, {
  name: 'password',
  hidden: true,
  conform: function (value) {
    return true;
  }
}, {
 name: 'passwordMasked',
hidden: true,
replace: '*',
conform: function (value) {
  return true;
 }
}], function (err, result) {
//
// Log the results.
//
console.log('Command-line input received:');
console.log('  username: ' + result.username);
console.log('  password: ' + result.password);
console.log('  passwordMasked: ' + result.passwordMasked);

});

// login
await page.waitForSelector("input[name='user[login]']")
await page.type("input[name='user[login]']", 'username')
await page.waitForSelector("input[name='user[password]']")
await page.type("input[name='user[password]']", 'password')
await page.click("input[type='submit']")
await page.waitForSelector("input[name='real_name']")
await page.click('a.d-none.d-md-flex')
await page.waitForTimeout(3000)
// capture screenshot and store it into screenshots directory.
await page.screenshot({ path: `screenshots/SEG_Report.jpeg` });
} catch (err) {
console.log(`❌ Error: ${err.message}`);
} finally {
 await browser.close();
 console.log(`\n SEG Report Captured and Saved!`);
}

}

captureScreenshot();

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Tiddzy
  • 1
  • 1
  • Put all of your puppeteer code into the prompt result callback or promisify the callback so you can await it. – ggorlen Jul 07 '22 at 22:36
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ggorlen Jul 07 '22 at 22:43

0 Answers0