Configure Puppeteer executablePath chrome in your local Windows - node.js

Puppeteer version : 1.11.0
Platform / OS version: Windows 10 pro
Node.js version: 12.6.6
When I did a local development test in windows, happen was problem in executablePath.
"Failed to launch chrome! spawn /usr/bin/chromium-browser ENOENT"
I saw for windows needs to get complete path. Otherwise cannot find chrome.exe
Default in code:
const browser = await puppeteer.launch({executablePath: '/path/to/Chrome'});
In windows it worked thus:
const browser = await puppeteer.launch({executablePath: 'C:\\your_workspace\\node_modules\\puppeteer\\.local-chromium\\win64-(version)\\chrome-win\\chrome.exe'});
In visual code suggest the path
Visual Code view explorer

You can also set the environment variable PUPPETEER_EXECUTABLE_PATH.
This is useful in conjunction with PUPPETEER_SKIP_CHROMIUM_DOWNLOAD set to true

Maybe this can help:
const osPlatform = os.platform(); // possible values are: 'darwin', 'freebsd', 'linux', 'sunos' or 'win32'
console.log('Scraper running on platform: ', osPlatform);
let executablePath;
if (/^win/i.test(osPlatform)) {
executablePath = '';
} else if (/^linux/i.test(osPlatform)) {
executablePath = '/usr/bin/google-chrome';
}

Related

Use node ssh into Runcloud gives a different version to what is actually installed/used by NVM on the server

I have a server on Runcloud on which I've used NVM and installed the version 16.14.2.
If I ssh via any SSH client and run node -v, I effectively get 16.14.2
Though when I wrote a script to ssh into the server and run the same command, I get 10.0
I was previously advised to create an alias, I tried to follow some steps I came across for that but it did not fix my issue. Further more, referencing the path to the desired version of npm inside nvm gives me an error that this version npm cannot run with node 10.0
npm does not support Node.js v10.0.0\n
Below is my code
import { NodeSSH } from 'node-ssh'
const ssh = new NodeSSH()
const { environment } = await inquirer.prompt([
{
name: 'environment',
message: `Environment?`,
type: 'input',
default: 'development'
}
])
if (build) {
const sshConfig = sshConfigs[environment]
console.log(chalk.yellow(`Connecting to ${sshConfig.host}...`))
await ssh.connect(sshConfig)
console.log(chalk.green('Connected'))
console.log(chalk.yellow('Executing release...'))
const nodePath = '~/.nvm/versions/node/v16.14.2/bin/node'
const npmPath = '~/.nvm/versions/node/v16.14.2/bin/npm'
console.log(await ssh.execCommand(`${npmPath} -v`))
ssh.dispose()
console.log(chalk.green('Release completed'))
}

open Module Doesn't Work for Full Browser Path

const open = require('open')
const main = async () => {
await open('https://google.com', {
app: {
name: 'C:\Program Files\Mozilla Firefox\firefox.exe'
}
});
}
main();
Why this code doesn't work on W10? Passing full path browser .exe as an argument works only under Linux? Is that why the creator mentions WSL specifically?
You may also pass in the app's full path. For example on WSL, this can
be /mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe for
the Windows installation of Chrome.
Source: https://www.npmjs.com/package/open

AWS Lambda Nodejs 12.x using Selenium: Chrome Driver not found

When I run my nodejs 12.x code calling selenium, I am getting the error below.
At the top level of my directory structure, I have my index.js file, a lib folder and a node_modules folder.
I am preparing the code on a Mac and then zipping it, uploading to S3 and running it in Lambda.
Where should my chrome and chromedriver executables be? I don't think I can use npm to install them as I think I need the linux versions for running in Lambda?
This is the error I am getting:
{
"errorType": "Error",
"errorMessage": "The ChromeDriver could not be found on the current PATH. Please download the latest version of the ChromeDriver from http://chromedriver.storage.googleapis.com/index.html and ensure it can be found on your PATH.",
"trace": [
"Error: The ChromeDriver could not be found on the current PATH. Please download the latest version of the ChromeDriver from http://chromedriver.storage.googleapis.com/index.html and ensure it can be found on your PATH.",
" at new ServiceBuilder (/var/task/node_modules/selenium-webdriver/chrome.js:232:13)",
" at getDefaultService (/var/task/node_modules/selenium-webdriver/chrome.js:321:22)",
" at Function.createSession (/var/task/node_modules/selenium-webdriver/chrome.js:695:44)",
" at createDriver (/var/task/node_modules/selenium-webdriver/index.js:155:33)",
" at Builder.build (/var/task/node_modules/selenium-webdriver/index.js:662:16)",
" at Runtime.exports.handler (/var/task/index.js:38:26)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
This is my code
'use strict';
exports.handler = async (event, context, callback) => {
var webdriver = require('selenium-webdriver');
var chrome = require('selenium-webdriver/chrome');
var builder = new webdriver.Builder().forBrowser('chrome');
var chromeOptions = new chrome.Options();
const defaultChromeFlags = [
'--headless',
'--disable-gpu',
'--window-size=1280x1696', // Letter size
'--no-sandbox',
'--user-data-dir=/tmp/user-data',
'--hide-scrollbars',
'--enable-logging',
'--log-level=0',
'--v=99',
'--single-process',
'--data-path=/tmp/data-path',
'--ignore-certificate-errors',
'--homedir=/tmp',
'--disk-cache-dir=/tmp/cache-dir'
];
chromeOptions.setChromeBinaryPath("/var/task/lib/chrome");
chromeOptions.addArguments(defaultChromeFlags);
builder.setChromeOptions(chromeOptions);
//*****this is the problem line****//
var driver = builder.build();
driver.get(event.url);
driver.getTitle().then(function(title) {
console.log("Page title for " + event.url + " is " + title)
callback(null, 'Page title for ' + event.url + ' is ' + title);
});
driver.quit();
};
Check this:
https://github.com/blackboard/lambda-selenium/blob/master/README.md
Welcome to the lambda-selenium project! The purpose of this project is to show how to use Selenium Webdriver with Amazon Web Services (AWS) Lambda compute service.
or
https://github.com/smithclay/lambdium/blob/master/README.md
Lambdium uses Selenium Webdriver with Headless Chromium to run Webdriver scripts written in JavaScript on AWS Lambda.
You can use chrome-aws-lambda package for running it headless in your lambda function , As per Documentation 512 MB of ram will be used https://www.npmjs.com/package/chrome-aws-lambda

Is there a way to launch a terminal window (or cmd on Windows) and pass/run a command?

Question
Is it possible to do the following?
open a new cmd.exe or terminal (on MacOS / Linux) window
pass / run a command, e.g. cd <path>
Problem
I can open cmd by running this command:
"$electron.shell.openItem('cmd.exe')"
But shell.openItem doesn't allow to pass the arguments / commands.
I tried using child_process but I couldn't make it work at all, it doesn't open a new terminal window:
const { spawn, exec } = require('child_process');
spawn('C:/Windows/System32/cmd.exe');
I also tried running the following command, but still nothing:
spawn( 'cmd.exe', [ '/c', 'echo ASDASD' ], { stdio: [0, 1, 2] } )
The only possible solution that I see is to create a command.bat:
start cmd.exe /K "cd /D C:\test"
And then use openItem:
"$electron.shell.openItem('command.bat')"
But that would only work on Windows
Solution
I finally found a way to do it on Windows:
var child_process = require('child_process');
child_process.exec("start cmd.exe /K cd /D C:/test");
Notes
You have to add the word start to open a new command window
Instead of cd /D C:/test you can specify any other command, e.g. python
/D is to make sure it will change the current drive automatically, depending on the path specified
/K removes the initial message
Don't use execSync it will lock the app until the terminal (command
prompt) window is closed
As for MacOS, looks like it's possible to do with osascript
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'
Here is a working example showing how to open a Terminal window at a specific path (~/Desktop for instance) on macOS, from a renderer script:
const { app } = require ('electron').remote;
const atPath = app.getPath ('desktop');
const { spawn } = require ('child_process');
let openTerminalAtPath = spawn ('open', [ '-a', 'Terminal', atPath ]);
openTerminalAtPath.on ('error', (err) => { console.log (err); });
It should be easy to adapt it to any selected atPath...
As for running other commands, I haven't found a way yet...
And here is the equivalent working code for Linux Mint Cinnamon or Ubuntu:
const { app } = require ('electron').remote;
const terminal = 'gnome-terminal';
const atPath = app.getPath ('desktop');
const { spawn } = require ('child_process');
let openTerminalAtPath = spawn (terminal, { cwd: atPath });
openTerminalAtPath.on ('error', (err) => { console.log (err); });
Please note that the name of the terminal application may be different, depending on the Linux flavor (for instance 'mate-terminal' on Linux Mint MATE), and also that the full path to the application can be explicitly defined, to be on the safe side:
const terminal = '/usr/bin/gnome-terminal';
HTH...

selinium-webdriver issue with Error: The geckodriver.exe executable could not be found on the current PATH

Hey I want to get screen shot with nodejs selinium-webdriver firefox
I am getting error like that : Error: The geckodriver.exe executable could not be found on the current PATH.
I set up the enviornment variable, but no luck
You need to set the path to the geckodriver.exe prior to creating the driver instance.
In Java:
System.setProperty("webdriver.gecko.driver", "./drivers/geckodriver.exe");//"<PATH TO LOCATION>\\chromedriver.exe");
I had some successful result with this process:
1° - Check if your webdriver(geckodriver, chromedriver, etc.) is in the correct path (if you don't know how to do it, check the link, https://www.youtube.com/watch?v=fj0Ud16YJJw). I think this video is a little old, because in npm the code information is different, updated, but it also serves as instruction.
2°- I changed my type of license(in package.json) from “ISC” to “MIT”, you can do this manually. And for my surprise, this change made my code go well.
const webdriver = require("selenium-webdriver");
const firefox = require("selenium-webdriver/firefox");
const { Builder, Browser, By, Key, until } = require("selenium-webdriver");
async function example() {
let driver = await new Builder().forBrowser(Browser.FIREFOX).build();
try {
await driver.get("https://www.google.com/ncr");
await driver.findElement(By.name("q")).sendKeys("Selenium", Key.RETURN);
await driver.wait(until.titleIs("webdriver - Google Search"), 1000);
} finally {
await driver.quit();
}
}
example();
And here we have another link that goes to selenium webdriver dependency in npm website(https://www.npmjs.com/package/selenium-webdriver) for more information.

Resources