cent os puppeteer code example
Example: setup puppeteer centos7
curl -sL https://rpm.nodesource.com/setup_10.x | sudo -E bash -
yum install nodejs
1
2
3
4
5
6
7
curl -sL https://rpm.nodesource.com/setup_10.x | sudo -E bash -
yum install nodejs
Step 2 :- Install Puppeteer In CentOS 7
npm i pupeteer
1
npm i pupeteer
Step 3 :- Install Chromium In CentOS 7
yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc
1
2
3
yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc
Step 4 :- Install puppeteer in CentOs7
npm install --save puppeteer
1
npm install --save puppeteer
Now Let’s Write a Sample Code to run Puppeteer in headless mode and name that file as test.js
'use strict';
const puppeteer = require('puppeteer');
const fs = require('fs');
(async () => {
console.info("Starting browser");
let browser;
try {
browser = await puppeteer.launch({});
} catch (e) {
console.info("Unable to launch browser mode in sandbox mode. Lauching Chrome without sandbox.");
browser = await puppeteer.launch({args:['--no-sandbox']});
}
console.info("Browser successfully started");
console.info("Closing browser");
await browser.close();
console.info("Done");
})();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
'use strict';
const puppeteer = require('puppeteer');
const fs = require('fs');
(async () => {
console.info("Starting browser");
let browser;
try {
browser = await puppeteer.launch({});
} catch (e) {
console.info("Unable to launch browser mode in sandbox mode. Lauching Chrome without sandbox.");
browser = await puppeteer.launch({args:['--no-sandbox']});
}
console.info("Browser successfully started");
console.info("Closing browser");
await browser.close();
console.info("Done");
})();
Step 5 :- Run the Code , you will see the following output
Setup Puppeteer In CentOS 7
running puppeteer centos7 without sandbox mode
Real problem starts here , We are not able to launch browser without --no-sandbox flag.
I Challenge most of you will face this problem while you setup puppeteer in Centos 7. SO Follow down this simple hack to solve this . I struggled a lot , I think you may or may not . But if you have reached this section of article then i firmly believe you have faced this as well.
For running Puppeteer in sandbox mode we need to do few changes as of now .
sudo mv chrome_sandbox chrome-sandbox
sudo chown root chrome-sandbox
sudo chmod 4755 chrome-sandbox
1
2
3
4
sudo mv chrome_sandbox chrome-sandbox
sudo chown root chrome-sandbox
sudo chmod 4755 chrome-sandbox
So what are we doing here ,
We are renaming the chrome_sandbox folder to chrome-sandbox as standard chromium searches for file with chrome-sandbox. Now run the test file again.
$ node test.js
$ node test.js
Starting browser
Browser successfully started
Closing browser
Done