Multiple browsers and profiles instance with puppeteer [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
I'm trying to find a way in puppeteer to open multiple instance with different Chrome profiles (to have different cookies in the same website), I found the way to do it but I'd like to do it in a for loop.
The way I'm doing its the next
const browser1 = await puppeteer.launch({
headless: false,
executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
args: ["--user-data-dir=C:/Users/Nico/AppData/Local/Google/Chrome/User Data/Profile 1"]
});
const page1 = await browser1.newPage();
await page1.goto("http://www.website.com");
const browser2 = await puppeteer.launch({
headless: false,
executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
args: ["--user-data-dir=C:/Users/Nico/AppData/Local/Google/Chrome/User Data/Profile 2"]
});
const page2 = await browser2.newPage();
await page2.goto("http://www.website.com");
So I'd like to find a way to do it in a for loop so I don't have to make each browser[i] in the code
I tried with
for (let i = 2; i < 20; i++) {
const browser[i] = await puppeteer.launch({
headless: false,
executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
args: ["--user-data-dir=C:/Users/Nico/AppData/Local/Google/Chrome/User Data/Profile ", [i]]
});
}
But doesn't seems to work

Related

why puppeteer does not type in some inputs

Somtimes puppeteer does not type in some input fields, to be specific, I tried to simply type something in a website's input filed called "https://webtor.io/", which has a single huge input field, I hope someone could help me with that specific example.
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://webtor.io/');
await page.type(`input[type="text"]`, 'something', { delay: 50 })
})();
This happens because when you go to the page the page has render the html and load up scripts, which ends up causing the delay and sometimes the text input is not loaded hence the fail.
await page.goto(''https://webtor.io/', {waitUntil: 'networkidle0'});
Check out this link for further details.
Puppeteer wait until page is completely loaded

Puppeteer and Playwright chrome headful bugs when making a screenshot

I am currently developing a node.js script that needs to launch a headful chromium instance using Puppeteer and then make a screenshot of a page every 3 seconds, this is my code :
const puppeteer = require('puppeteer');
async function init (){
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.goto('https://example.com');
screenshot(page)
};
async function screenshot(page){
let buffer = await page.screenshot();
let imageBuffer = buffer.toString('base64');
// save imageBuffer to database
setTimeout(screenshot, 3000, page)
}
My current issue is that I need the user to still be able to normally navigate on the browser and on his computer but this impossible as :
The page lags when making the screenshot as you can see on the following video : https://youtu.be/Tl2w-qKckkc
The browser window focuses and goes on top of all the windows when making the screenshot.
I also tried using Playwright but the same bug occurs when using it with chromium. Can someone please help.
In Playwright, do the following:
// Affects all the platforms.
const page = await browser.newPage({ viewport: null });
// Local fix for those using Apple hardware with Retina displays.
const page = await browser.newPage({ deviceScaleFactor: 2 });
I posted a detailed reply at https://github.com/microsoft/playwright/issues/2576. Please feel free to follow up and ask questions / request features there!

Puppeteer launch a new tab in current window, not new window

I've tried just about everything and can't seem to figure out how to get Puppeteer to work in my current browser window (Where I'm logged in to Chrome) rather than a new cache-less logged out browser. Here's my current config setting up everything. I've tried starting chrome prior with remote debug port, loading user data in args for launching puppeteer, launching both Chromium and my current Chrome installation path, etc. Here's my current code:
const opts = {
logLevel: 'info',
output: 'json'
};
const chrome = await chromelauncher.launch( {port:9222 });
opts.port = chrome.port;
// Connect to it using puppeteer.connect().
const resp = await util.promisify(request)(`http://localhost:${opts.port}/json/version`);
const {webSocketDebuggerUrl} = JSON.parse(resp.body);
const browser = await puppeteer.connect({browserWSEndpoint: webSocketDebuggerUrl,
args: ["--disable-extensions"]});
const page = await browser.newPage();
await page.setViewport({ width: 1366, height: 768});
I've run out of resources to look, if something looks off please let me know. Thanks!
You have to use a userDataDir to reuse the cache.
puppeteer.launch({
userDataDir: 'PATH TO DATA FOLDER',
})
You can find your data directory here,
Windows 7, 8.1, and 10: C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default
Mac OS X El Capitan: Users/<username>/Library/Application Support/Google/Chrome/Default
Linux: /home/<username>/.config/google-chrome/default
Another way is to open chrome://version and pick the path from there,
Now, remove the Default and you will get your data directory,
[Profile Path] C:\Users\Alice\AppData\Local\Google\Chrome\User Data\Default
[User Data Dir] C:\Users\Alice\AppData\Local\Google\Chrome\User Data
So the code will look like,
puppeteer.launch({
userDataDir: `C:\Users\Alice\AppData\Local\Google\Chrome\User Data`,
// <-- notice I used backtick to avoid writing backslashs
})
Learn more about data directory here.
Another interesting argument is the --profile-directory, You can name a profile and use that.
--profile-directory=Default
Not sure if this is useful to your case as I use the Chromium browser and not my system chrome browser but I save the Chromium setting inside the project PeaceOut.
I'm still learning how to use puppeteer so might not be doing everything the best way.
const browser = await puppeteer.launch({
headless: false,
devtools: true,
// slowMo: 250 // slow down by 250ms
// executablePath <string> Path to a Chromium or Chrome executable to run
userDataDir: 'C:\\Users\\TeDev\\Scrape\\PeaceOut\\bdata'
// userDataDir <string> Path to a User Data Directory.
});
const page = await browser.pages();
await page[0].setViewport({ width: 1280, height: 1080 })
console.log(`Trying to access ${URL}`);
await page[0].goto(URL); // use tab 0, so Chromium doesn't show a blank tab.
Add --new-window argument when launching

Puppeteer "Unable to move ShaderCache" when attempting to load Chrome profile

I've had quite a few issues trying to get Puppeteer to run in an existing chrome window with user data. I've tried simplifying things down and here is my current code. I don't know what else to do as i've tried everything related to the issue:
const browser = await puppeteer.launch({
headless: false,
userDataDir: 'C:\\Users\\me\\AppData\\Local\\Google\\Chrome\\User Data'
})
const page = await browser.newPage();
This causes the following error:
error Error: Failed to launch chrome!
[8084:14860:0321/091939.752:ERROR:cache_util_win.cc(19)] Unable to move the cach
e: 0
[8084:14860:0321/091939.752:ERROR:cache_util.cc(140)] Unable to move cache folde
r C:\Users\me\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache
to C:\Users\me\AppData\Local\Google\Chrome\User Data\ShaderCache\old_GP
UCache_000
[8084:14860:0321/091939.752:ERROR:disk_cache.cc(184)] Unable to create cache
[8084:14860:0321/091939.752:ERROR:shader_disk_cache.cc(622)] Shader Cache Creati
on failed: -2
The issue is related to an open Chrome process. Make sure you close all of them in the task manager and the following will work:
browser = await puppeteer.launch({
headless: false,
executablePath: `C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe`,
userDataDir: `C:\\Users\\Marwan\\AppData\\Local\\Google\\Chrome\\User Data`,
});

Puppetter times out when opening a URL

Background
I am running Puppeteer in an application locally that works fine. When I move it to a production debian server, it times out at the
page.goto(url) function.
Example
I have tried a bunch of different suggestions online. In the example below you will see a few options I have tried that were suggested on line. I have tried these all alone and in different combinations with each other. Yes I am that desperate at this point.
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--ignore-certificate-errors',
'--ignore-certificate-errors-spki-list',
'--user-data-dir']});
const page = await browser.newPage();
await page.goto(
`https://example.com/${template}?data=${JSON.stringify(req.body)}`, {waitUntil: 'networkidle0'}
);
page.goto(url) works locally, but fails when running on server.
Question
Why is page.goto() failing on the server and is there any work around?
page.setDefaultNavigationTimeout is your option
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--ignore-certificate-errors',
'--ignore-certificate-errors-spki-list',
'--user-data-dir']});
const page = await browser.newPage();
page.setDefaultNavigationTimeout(3600); // 1 hour
await page.goto(
`https://example.com/${template}?data=${JSON.stringify(req.body)}`, {waitUntil: 'networkidle2'}
);
reference https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetdefaultnavigationtimeouttimeout

Categories