Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
1
Indexable
Never
console.time('Code execution time');

const puppeteer = require('puppeteer-core');

const launchBrowsers = async (numInstances) => {
  const browsers = [];

  for (let i = 1; i <= numInstances; i++) {
    const browser = await puppeteer.launch({
      headless: true,
      executablePath: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
      userDataDir: `C:\\Users\\admin\\AppData\\Local\\Google\\Chrome\\User Data\\Profile ${i}`
    });
    const page = await browser.newPage();

    await page.goto('https://finance.yahoo.com/');
    await page.waitForSelector('input[name="yfin-usr-qry"]');

    browsers.push({ browser, page });
  }

  return browsers;
};

const typeDoggo = async (page, delay) => {
  for (let i = 0; i < 5; i++) {
    await page.type('input[name="yfin-usr-qry"]', 'DOGGO ');
    console.log('typed doggo')
    await page.waitForTimeout(delay);
  }
}

(async () => {
  const numInstances = 3
  const browsers = await launchBrowsers(numInstances);

  await setTimeout(() => {
    console.log('waited 500 seconds')
  }, 500000)

  // Use Promise.all to run the two functions at the same time
  await Promise.all(browsers.map(({ page }, i) => typeDoggo(page, (i + 1) * 2000)));

  // Close the browsers
  await Promise.all(browsers.map(({ browser }) => browser.close()));

  console.timeEnd('Code execution time');
})();