How to use the connect function from puppeteer

Find comprehensive JavaScript puppeteer.connect code examples handpicked from public code repositorys.

puppeteer.connect establishes a connection to an existing browser instance.

602
603
604
605
606
607
608
609
610
611
    'browserstack.accessKey': browserstack_password || 'YOUR_DEFAULT_ACCESS_KEY',
    'browserstack.local': 'true'  // setting this capability to true would inform BrowserStack that publicly inaccessible URLs have to be resolved through your local network using the tunnel connection created by 'browserstack-local' node package
};

// use `.connect()` to initiate an Automate session on BrowserStack
const browser = await puppeteer.connect({
    browserWSEndpoint: `wss://cdp.browserstack.com/puppeteer?caps=${encodeURIComponent(JSON.stringify(capabilities))}`,
});

await start_dynamic_analysis(browser, webpage_path, website_url, website_data_directory);
fork icon6
star icon32
watch icon2

+ 5 other calls in file

96
97
98
99
100
101
102
103
104
105

await this.authStrategy.beforeBrowserInitialized();

const puppeteerOpts = this.options.puppeteer;
if (puppeteerOpts && puppeteerOpts.browserWSEndpoint) {
    browser = await puppeteer.connect(puppeteerOpts);
    page = await browser.newPage();
} else {
    const browserArgs = [...(puppeteerOpts.args || [])];
    if (!browserArgs.find(arg => arg.includes('--user-agent'))) {
fork icon1
star icon5
watch icon0

How does puppeteer.connect work?

puppeteer.connect() is a method provided by the Puppeteer library that creates a connection to an existing browser instance from a Node.js process, allowing the user to control the browser programmatically. Once the connection is established, the user can interact with the browser instance using the Browser object returned by this method. The puppeteer.connect() method takes an object containing connection options such as the browserWSEndpoint URL, and returns a promise that resolves with a Browser object.

91
92
93
94
95
96
97
98
99
100
const { mAPI } = this;

console.log('launching multilogin browser...');
const ws = (await mAPI.start(profileId)).value;

this.browser = await puppeteer.connect({
	browserWSEndpoint: ws,
	slowMo: 10,
});
// await utils.wait(5);
fork icon0
star icon3
watch icon0

8
9
10
11
12
13
14
15
16
17
  ignoreHTTPSErrors: opts.ignoreHttpsErrors,
  sloMo: config.DEBUG_MODE ? 250 : undefined,
};
if (config.BROWSER_WS_ENDPOINT) {
  browserOpts.browserWSEndpoint = config.BROWSER_WS_ENDPOINT;
  return puppeteer.connect(browserOpts);
}
if (config.BROWSER_EXECUTABLE_PATH) {
  browserOpts.executablePath = config.BROWSER_EXECUTABLE_PATH;
}
fork icon752
star icon0
watch icon126

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const puppeteer = require("puppeteer");

(async () => {
  const browserURL = "http://localhost:9222"; // The URL to connect to an existing instance of Chrome or Chromium.
  const browser = await puppeteer.connect({ browserURL });

  // Do something with the browser object, like create a new page and navigate to a URL.
  const page = await browser.newPage();
  await page.goto("https://www.google.com");

  // Close the browser when done.
  await browser.close();
})();

In this example, puppeteer.connect is used to create a connection to an existing instance of Chrome or Chromium that is already running with remote debugging enabled. This is done by passing in the URL of the remote browser instance to the browserURL parameter. Once the connection is established, you can use the browser object to interact with the remote browser just like you would with a locally running instance. In this example, a new page is created and navigated to a URL using the browser.newPage and page.goto methods. Finally, the browser instance is closed using the browser.close method.

357
358
359
360
361
362
363
364
365
366
//  and return 0. If unsuccessful, return -1, terminating the application.
let url = "http://127.0.0.1:9222/json/version"
try {
    let resp = await needle("get", url);
    Log(resp.body.webSocketDebuggerUrl)
    browser = await puppeteer.connect({
        browserWSEndpoint: resp.body.webSocketDebuggerUrl,
        defaultViewport: null
    });
    dayPage = await browser.newPage();
fork icon0
star icon0
watch icon1

+ 6 other calls in file

17
18
19
20
21
22
23
24
25
26
    ? `${config.browserUrl}:${config.debugport}`
    : undefined,
};

msg.puppeteer = {
  browser: await puppeteer.connect(launchOptions),
};

this.status({
  fill: "grey",
fork icon0
star icon0
watch icon1

+ 29 other calls in file

55
56
57
58
59
60
61
62
63
64
    } else if ( request.options.browserWSEndpoint ) {
        options.browserWSEndpoint = request.options.browserWSEndpoint;
    }

    try {
        browser = await puppet.connect( options );

        remoteInstance = true;
    } catch (exception) { /** does nothing. fallbacks to launching a chromium instance */}
}
fork icon0
star icon0
watch icon1

+ 15 other calls in file

6
7
8
9
10
11
12
13
14
}

async init() {
  try {
    if (process.env.NODE_ENV == "production") {
      super.browser = await puppeteer.connect({
        browserWSEndpoint: "ws://139.59.224.25:3000",
        slowMo: 10,
      });
fork icon0
star icon0
watch icon1

+ 9 other calls in file