Executing Puppeteer from PHP suddenly stopped working - javascript

We had not done any recent upgrades.
We are trying to run this javascript code (createpdf.js)
const puppeteer = require('/home/glossyadmin/node_modules/puppeteer');
const InvoiceNumber = process.argv[2];
const PDFdir = __dirname + '/../email/invoice/';
(async () => {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
const page = await browser.newPage();
await page.goto('http://localhost:8080/admin/ajax/invoice?InvoiceNumber=' + InvoiceNumber + '&Print=Print', {waitUntil: 'networkidle2'});
await page.pdf({path: PDFdir + InvoiceNumber + '.pdf', format: 'A4'});
await browser.close();
})();
We have been calling the code above with this PHP script:
$Vars = json_decode(urldecode(#$argv[1]), true);
$InvoiceNumber = $Vars['InvoiceNumber'];
$createScript = $_SERVER['DOCUMENT_ROOT'] . '/admin/workers/createpdf.js';
exec("node $createScript 2>&1 ".escapeshellarg($InvoiceNumber), $arrayReturn);
It has been working but lately it just stopped. The code seems good (no PHP errors in the error log) but it just is no longer calling / executing the createpdf.js script. And we have verified that node is in fact installed.
We have not upgraded PHP, Node, or any other updates. We've tried changing permissions of files and directories. Nothing changes. Slamming our head into a wall here.

After continuing to dig and finding out more and more people were having this issue with little to no fixes. I found another thread on stackoverflow that talked about somebody that had upgraded their version of puppeteer and it suddenly stopped working.
We had not done any upgrades to puppeteer, node, or the system at all but still wasn't working. I took the very simple advice in the comments of this thread: Puppeteer script suddenly stopped working after upgrading Puppeteer
Delete the entire node_modules directory and reinstall puppeteer.
Not sure why exactly but it has worked. I wish there was more information out there. Luckily it's a quick install.

Related

Beginner problem with crashing code to lookup class or text on website

I write this code to check if on my website is a text and if its not then it should send me a notification thru slack. When I run it on VSC it crushes after some time like maybe 15 min or something like that.
I want to make it nice to put it on a server and run it remotly but need to be sure that will not crash every so often. I want to use it to check some websites for changing information on them and if they will change or be gone then send me notification. Best bit it works but crashes and don't know why :(
Can someone maybe help or pinpoint what it can be the problem? It will be better that this tool can just see text instead of class but I don't know how to do that.
//Puppeteer library
const pt= require('puppeteer')
const axios = require('axios')
process.setMaxListeners(0);
async function getText(){
//launch browser in headless mode
const browser = await pt.launch()
//browser new page
const page = await browser.newPage()
//launch URL
await page.setDefaultNavigationTimeout(0);
//website
await page.goto('https://mieciusio.pl/kontakt.html')
//identify element
if (await page.$("[class='p-style btn-resize-mode label-bloc-2-style label-1-style']"))
console.log("found")
else //console.log("not found")
axios.post(' https://hooks.slack.com/services/MYUniqeID', {text: 'Its changed'})
}
setInterval(getText, 12000)
Try to find it online on YT but it's hard I was looking in a lot of tut's but can't find right one to work on finding text on website or not to crush because I don't know why crashes.

Browser console and Puppeteer cannot find certain selector

I am trying to build a simple scraper for this website by using puppeteer.
The code goes as follows:
const browser = await puppeteer.launch({
headless: false
});
const page = await browser.newPage();
let pagelink = "https://www.speisekarte.de/berlin/restaurants?page=1"
await page.waitFor(3 * 1000);
await page.goto(pagelink);
await page.waitFor(3 * 1000);
await page.waitForSelector("#notice")
However, I cannot access the overlay notice for the cookies which should have the Id "notice".
This does not work either for await page.waitForSelector("#notice")
in my puppeteer code.
Nor with document.getElementById("notice") in Chromium, if I use the console of Chromium during the session manually. Also, it does not work, if I use it in Firefox's console. Funnily enough, chunks like
document.querySelectorAll("button")
work as expected. I checked with a colleague and she can access the element using the above mentioned queries in her Chrome and in her Firefox browser. She also uses a Mac. Any idea, what is happening here? Any help would be much appreciated.

How to get element by resource id?

I have spent some hours trying to search an element by id using appium javascript client with no luck. Have found some other posts here in stack overflow stating it works, but it doesn not work for me. It seems that I could use something like:
var buttonEl = await driver.findElement(By.id("resourceid"));
but I always get an error saying:
InvalidSelectorError: Locator Strategy 'css selector' is not supported for this session
Here the source code:
"use strict";
var wd = require("selenium-webdriver"),
By = wd.By,
until = wd.until;
// Setting Desired Capabilities.
var desiredCaps = {
platformName: "Android",
deviceName: "a3ae1c63",
appPackage: "com.mypackage",
appActivity: ".Main",
browserName: '',
noReset: true,
newCommandTimeout: 1000000
};
async function test() {
//Initiating the Driver
let driver = await new wd.Builder().usingServer("http://localhost:4723/wd/hub").withCapabilities(desiredCaps).build();
var buttonEl = await driver.findElement(By.id("id/contact_selector"));
buttonEl.click();
}
test();
I'm pretty confused now. I understand that it seems that I can't use the find element by id in an android session but on the other hand I have read about people using it successfully.
Any help would be greatly appreciated.
I think you are using selenium arguments for the locator strategy. Please readedocumentation of appium selector here http://appium.io/docs/en/commands/element/find-elements/index.html#selector-strategies.
try this
var buttonEl = await driver.element("id", "resource-id");
Finally I was able to fix my problem. The main problem was that I wasn't using the needed web driver. I switched to webdriverio and everything is working now.
Better place to start is to check examples in appium github. They have some examples for every driver. Code is a bit outdated though.

uploading a file using puppeteer browserWSEndpoint

I am trying to upload a file using puppeteer and browserWSEndpoint, the error message I am getting is
"Uncaught (in promise) Error: File chooser handling does not work with multiple connections to the same page".
Here is my code:
const puppeteer = require('puppeteer');
async function getTest() {
const browser = await puppeteer.connect({
browserWSEndpoint: 'wss://chrome.browserless.io'
});
const page = (await browser.pages())[0];
await page.goto('https://someWebSite');
//DO STUFF
console.log("before upload"); //code runs until here
const [fileChooser] = await Promise.all([page.waitForFileChooser(),page.click('#uploadTrigger'),]);
await fileChooser.accept(['C:\\myProgram\\pic.jpg']);
await page.click('#edit-submit');
}
getTest().then(console.log);
I must mention that if I don't use browserWSEndpoint, and use this code at the beginning instead, everything works fine.
const browser = await puppeteer.launch({headless: false, defaultViewport:null});
Honnestly I am pretty lost with browserWSEndpoint, I used info from this post How to run Puppeteer code in any web browser?
which led me to browserless.io, copied the code and it works.
Now this is my precise question, my error indicates does not work with multiple connections to the same page. How exactly am I connecting with multiple connections? Maybe I can resolve this issue and then I could use const [fileChooser].
My main issue is that I need to upload a file, using browserless
Others seem to have the same problem according to https://github.com/GoogleChrome/puppeteer/issues/4783, but using chromuim is not an option if I want to use browserless
If you are the only client connected to that browser you must be connected to a browser that doesn't support the fileChooser. You should connect to a Chromium 77.0.3844.0 (r674921) or higher.

Puppeteer fails to execute from Crontab but works when executing manually from terminal

I want to make a Node.js Puppeteer cronjob, but Puppeteer breaks without any errors only when executed from a cronjob.
The script I'm trying works perfectly when executed manually (by Terminal) from it's root folder, but something seems to go wrong whenever I try to execute it as a cronjob and the execution stops.
I made some console.logs to help me situate the problem and found out that the script's execution breaks right at const browser = await puppeteer.launch().
Anything after it just doesn't execute.
See code below to get what I'm talking about:
'use strict'
console.log('Node script starts!');
const puppeteer = require('puppeteer');
(async () => {
console.log('test 1');
//Code stops here
const browser = await puppeteer.launch();
console.log('test 2');
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({
path: '0_ScreenShot.png'
});
await browser.close();
})();
So executing the above code manually through SSH by doing:
[site#server ~]$ cd public_html/test/
Then:
[site#server test]$ nohup node ctest.js > out.log &
My out.log file outputs the following and saves a screenshot under /test/0_ScreenShot.png which is the expected result:
Node script starts!
test 1
test 2
But for some reason, which is why I'm here, executing that same file through a cronjob does not work, the out.log prints only the following:
Node script starts!
test 1
The cronjob in question is setup from the cPanel as :
./bin/node ./public_html/test/ctest.js > ./public_html/test/out.log &
I thought it was a path problem and tried to be as absolute as possible by changing the Puppeteer initialization to this but without luck:
const browser = await puppeteer.launch({
executablePath: '/home/site/public_html/test/node_modules/puppeteer/.local-chromium/linux-654752/chrome-linux/chrome'
});
I even downloaded a fresh Chromium and used it but still nothing:
const browser = await puppeteer.launch({
executablePath: '/home/site/public_html/test/node_modules/chromium/lib/chromium/chrome-linux/chrome'
});
I used a bash script (shell_node.sh) to execute the node script but the same issue persisted:
#!/usr/bin/env sh
nohup ./bin/node ./public_html/test/ctest.js > ./public_html/test/out.log &
Cron:
bash ./public_html/test/shell_node.sh
I also tried executing the script by PHP using the exec() function, nothing still.
I looked everywhere for a solution, but it feels like I'm the first person ever to have come across this specific issue.. which is worrying, but I'm hoping someone from here might answer me.
Thank you for your read and your time.

Categories