How can I run a flash game in headless chrome using puppeteer? I'm trying to screenshot this flash game but the game doesn't run and is replaced by "Couldn't load plugin" text.
Here's the relevant code I used to generate the screenshot and its output, running in ubuntu on windows subsystem linux:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
const page = await browser.newPage();
await page.setViewport({width: 1243, height: 882});
await page.goto('http://www.bigfuntown.com/Game-59.html');
await page.screenshot({path: 'game.png'});
await browser.close();
})();
In modern Chrome versions flash is blocked by default and requires user interaction to be enabled.
As this was problematic to automate I've made a puppeteer wrapper for this purpose: puppeteer.setExtra({allowFlash: true})
Note that headless: false is still required due to puppeteer limitations.
Related
I have a small javascript setup to convert html to pdf using the javascript library puppeteer.
Hosting the service by opening the command panel and starting node index.js everything works fine. The express-api hosts the service under the predefined port and requesting the service I get the converted PDF back.
Now, installing the javascript as Windows-Service by using the library node-windows and requesting the service, I get the following error message:
Failed to launch the browser process!
Now I'm not sure where to search for the root cause. Is it possible that this could be a permission issue?
Following my puppeteer javascript code :
const ValidationError = require('./../errors/ValidationError.js')
const puppeteer = require('puppeteer-core');
module.exports = class PdfService{
static async htmlToPdf(html){
if(!html){
throw new ValidationError("no html");
}
const browser = await puppeteer.launch({
headless: true,
executablePath: process.env.EDGE_PATH,
args: ["--no-sandbox"]
});
const page = await browser.newPage();
await page.setContent(html, {
waitUntil: "networkidle2"
});
const pdf = await page.pdf({format: 'A4',printBackground: true});
await browser.close();
return pdf;
}
}
I am trying to open Chromium with extensions, but I cannot figure out how to do this. When chromium opens there are no extensions installed.
I tried to open with '--enable-remote-extensions', --load-extension=`, I tried to drag and drop the .crx into chromium extensions panel, but nothing worked.
I've got "An error has occurred
Installation is not enabled" and "Package is invalid: 'CRX_REQUIRED_PROOF_MISSING'
Could you help me with a working example ?
Thanks!
After lots of trial and error, I've solved this issue.
Below is the working code and I hope it will help someone else.
const puppeteer = require('puppeteer');
const extentionPath = "C:\\Users\\<YOUR_USERNAME>\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1\\Extensions\\<LONG_STRING_EXTENTION_ID>\\<EXTENTION_VERSION>"
(async () => {
const customArgs = [
`--start-maximized`,
`--load-extension=${extentionPath}`
];
const browser = await puppeteer.launch({
defaultViewport: null,
headless: false,
ignoreDefaultArgs: ["--disable-extensions", "--enable-automation"],
args: customArgs,
});
const page = await browser.newPage();
await page.goto(`https://google.com/`);
await page.waitForNavigation();
await page.close();
await browser.close();
})();
When I try to run node app.js, I get error:
the message is Failed to launch the browser process! spawn
/Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app
EACCES
What I did
I checked the folder at /Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app and the file is not zipped. It can be run.
Note:
If I try to execute without the path, it works, but
I would like to use either Chrome or Chromium to open a new page.
const browser = await puppeteer.launch({headless:false'});
const express = require('express');
const puppeteer = require('puppeteer');
const app = express();
(async () => {
const browser = await puppeteer.launch({headless:false, executablePath:'/Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app'});
const page = await browser.newPage();
await page.goto('https://google.com', {waitUntil: 'networkidle2'});
})().catch((error) =>{
console.error("the message is " + error.message);
});
app.listen(3000, function (){
console.log('server started');
})
If you navigate to chrome://version/ page in this exact browser, it will show the Executable Path which is the exact string you need to use as executablePath puppeteer launch option.
Usually, chrome's path looks like this on MAC:
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome
Or something like this if chromium is located in your node_modules folder:
/Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app/Contents/MacOS/Chromium
Now if you compare the string you used for executablePath: it differs from the one retrieved with the method mentioned above. Exactly the /Contents/MacOS/Chromium should be added to the end of the current path to make it work.
Note: the chromium bundled with puppeteer is the version guaranteed to work together with the actual pptr version: if you plan to use other chrome/or chromium-based browsers you might experience unexpected issues.
Following up on #theDavidBarton:
Chromium which was shipped with Puppeteer did not work, but the Chrome installation on my MacBook did work.
OS: OS-X 10.15.7 (Catalina)
Node version: v14.5.0
Failed code:
const browser = await puppeteer.launch({
headless: true,
executablePath: "/users/bert/Project/NodeJS/PuppeteerTest/node_modules/puppeteer/.local-chromium/mac-818858/chrome-mac/Chromium.app/Contents/MacOS/Chromium"
});
Successful code:
const browser = await puppeteer.launch({
headless: true,
executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
});
Full code, Just the first example on the Puppeteer website:
const puppeteer = require('puppeteer');
(async () => {
try {
const browser = await puppeteer.launch({headless: true, executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"});
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
} catch (err) {
console.log(err);
}
})();
And, yes, I got the Screenshot !! :-)
Using location-chrome: https://www.npmjs.com/package/locate-chrome
const locateChrome = require('locate-chrome');
const executablePath = await new Promise(resolve => locateChrome(arg => resolve(arg)));
const browser = await puppeteer.launch({ executablePath });
I'm trying to use the Devtools Protocol with Puppeteer, but it throws the following error:
TypeError: page.target is not a function
This is my code:
const puppeteer = require('puppeteer');
(async() => {
// Use Puppeteer to launch a browser and open a page.
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
// Create a raw DevTools protocol session to talk to the page.
const session = await page.target().createCDPSession();
await page.goto('https://www.google.com');
})();
Am I missing something?
Make sure you're using the latest version of Puppeteer.
"dependencies": {
"puppeteer": "latest"
}
I installed the Tor Expert Bundle and I would like to run it with Puppeteer.
I try:
const browser = await puppeteer.launch({headless: false,args:['--proxy-server="socks5://127.0.0.1:9050"']});
But I get the error ERR_NO_SUPPORTED_PROXIES. I can run it with a normal Chrome browser.
There's an opened bug in chromium regarding more complex configurations for proxy in headless mode (Source). There has not been any activity since July 2017.
However, I've been able to run Puppeteer (1.3.0) with a headless chromium and SOCKS5 proxy configuration.
const browser = await puppeteer.launch({args: ['--proxy-server=socks5://127.0.0.1:1337']});
Try updating Puppeteer, which also updates the bundled Chromium, and run again. Also seems like you might have a typo: remove the " between socks5://127.0.0.1:9050.
Looks like puppetteer can only by run with proxy with no headless mode
Based on Running Puppeteer with Tor.
/**************************************************************************
* IMPORTS
***************************************************************************/
const puppeteer = require('puppeteer')
/**************************************************************************
* DEMOS > USING PUPPETEER BEHIND TOR
* BASED ON https://medium.com/#jsilvax/running-puppeteer-with-tor-45cc449e5672
***************************************************************************/
;(async () => {
const browser = await puppeteer.launch({
args: ['--proxy-server=socks5://127.0.0.1:9050'],
headless: false,
})
const page = await browser.newPage()
await page.goto('https://check.torproject.org/')
const isUsingTor = await page.$eval('body', (el) =>
el.innerHTML.includes('Congratulations. This browser is configured to use Tor')
)
if (!isUsingTor) {
console.log('Not using Tor. Closing...')
return await browser.close()
}
console.log('Using Tor. Continuing... ')
// Now you can go wherever you want
await page.goto('https://www.facebook.com/')
// You would add additional code to do stuff...
// Then when you're done, just close
await browser.close()
})()