0

I am using puppeteer and express js to make an api. I have done my code stuff and now i am testing the api on postman but i have a problem which is i could not send multiple scripts at once i mean i can send but just returning the last one.So what is the problem.


const express = require("express");
const app = express();
app.use(express.json())
const puppeteer = require("puppeteer");

const port = 3000;

let browser = null;

var target_url = ""

app.post('/get/script_result', async (req, res) => {

  if (browser == null) browser = await puppeteer.launch({
    headless: false, defaultViewport: null, args: ['--disable-infobars', '--disable-notifications', '--disable-default-apps',
      '--no-sandbox', '--mute-audio', '--ignore-certificate-errors', '--disable-features=LookalikeUrlNavigationSuggestionsUI'], ignoreHTTPSErrors: true
  });

  try {

  
  var page = await browser.newPage()

  await page.goto(req.body.target_url);

  var script = ""

  var script_delay = ""

  await page.waitForTimeout(`${req.body.script_delay}`)

 const handle = await page.evaluate(`${req.body.script}`)

 
  res.send(handle)
  res.end()

  
  //await page.close();

  return res.status(200)


  }
  catch (e) {
    console.log(e);

    res.send(e)

    res.status(500)

  }

})


app.listen(port, () => {
  console.log(`app is running on port: ${port}`);
});
bitoiu
  • 6,893
  • 5
  • 38
  • 60
  • Why dont you use this library I have build, it can do what you want https://www.npmjs.com/package/puppeteer-express – Alen.Toma Aug 24 '22 at 12:31
  • Could you elaborate on what you mean by 'multiple scripts'? – paddotk Aug 24 '22 at 12:32
  • "script" : "document.querySelector('#yDmH0d > c-wiz.SSPGKf.Czez9d > div > div > div.tU8Y5c > div.wkMJlb.YWi3ub > div > div.qZmL0 > c-wiz:nth-child(2) > div > section > header > div > div:nth-child(2) > button').click(); document.querySelector('#yDmH0d > c-wiz.SSPGKf.Czez9d > div > div > div.tU8Y5c > div.wkMJlb.YWi3ub > div > div.qZmL0 > c-wiz:nth-child(2) > div > section > div > div.bARER').innerText; document.querySelector('#yDmH0d > div.VfPpkd-Sx9Kwc.cC1eCc.UDxLd.PzCPDd.HQdjr.VfPpkd-Sx9Kwc-OWXEXe-FNFY6c > div.VfPpkd-wzTsW > div > div > div > div > div.t8iiQe > button').click(); " – Harun Ünal Aug 24 '22 at 13:11
  • as you see my script is those lines first one is until the click(); the other one is starting from document. now my problem is second script is not executing – Harun Ünal Aug 24 '22 at 13:13
  • Please [edit] the post to add code. Maybe check out [this setup](https://stackoverflow.com/a/67910262/6243352) for sharing a Puppeteer browser between requests? – ggorlen Aug 24 '22 at 14:11

0 Answers0