最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Puppeteer launch a new tab in current window, not new window - Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question asked Mar 20, 2019 at 18:05 apriscottapriscott 911 silver badge5 bronze badges 2
  • loading user data in args for launching puppeteer, it says you tried this. However, you did not mention where are you trying this and why you are using --disable-extensions args or using a browserWSpoint. :) – Md. Abu Taher Commented Mar 20, 2019 at 18:13
  • Hi! I loaded it in args with --user-data-dir and as an argument to puppeteer as well – apriscott Commented Mar 20, 2019 at 18:32
Add a ment  | 

3 Answers 3

Reset to default 4

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

发布评论

评论列表(0)

  1. 暂无评论