Hey everyone, I’m stuck trying to run Puppeteer from my Laravel controller. I’ve got this JavaScript code that works fine when I run it directly in Node, but I can’t figure out how to make it work from my Laravel app.
Here’s what I’m trying to do:
$puppeteerScript = <<<EOD
const webBot = require('webBot');
(async function() {
const crawler = await webBot.init({visible: true});
const webpage = await crawler.createTab();
await webpage.navigate('https://example.com');
const submitBtn = await webpage.findElement('button.submit');
await submitBtn.trigger('click');
await webpage.wait(5000);
// More actions...
await crawler.shutdown();
})();
EOD;
$executor = new ScriptRunner(['node', '-e', $puppeteerScript]);
$executor->execute();
$result = $executor->getResult();
When I run just the JavaScript part in Node, it opens a browser and does what I want. But when I try to run it from Laravel, nothing happens. I think I’m not calling the JavaScript correctly from PHP. Any ideas on what I’m doing wrong or how to fix this? Thanks!