apriscott
apriscott

Reputation: 91

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!

Upvotes: 1

Views: 4991

Answers (3)

Pan Alexey
Pan Alexey

Reputation: 1

Add --new-window argument when launching

Upvotes: 0

Hellonearthis
Hellonearthis

Reputation: 1762

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.

Upvotes: 1

Md. Abu Taher
Md. Abu Taher

Reputation: 18826

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,

enter image description here

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

Upvotes: 4

Related Questions