Connecting Browsers in Puppeteer

10,511

The answer is Yes and No.

You can connect to an existing using the connect function:

const browserURL = 'http://127.0.0.1:21222';
const browser = await puppeteer.connect({browserURL});

But, if you want to use those 2 lines you need to launch Chrome with the "--remote-debugging-port=21222 argument.

Share:
10,511
Jak
Author by

Jak

Updated on June 14, 2022

Comments

  • Jak
    Jak almost 2 years

    Is it possible to connect a browser to puppeteer without instantiating it in puppeteer? For example, running an instance of chromium like a regular user and then connecting that to an instance of puppeteer in code?

  • Jak
    Jak about 5 years
    Thanks, very helpful. How about if I want to launch with Chromium? Would I still have to launch with any added arguments?
  • hardkoded
    hardkoded about 5 years
    @Jak yes, you always need to tell Chrome that he has to "open the doors" for remote debugging.
  • Jak
    Jak about 5 years
    Alright that makes sense. I am having some trouble finding any sort of documentation to help identify the appropriate browserURL for my instance of Chrome. Do you have any suggestions?
  • hardkoded
    hardkoded about 5 years
    @Jak If you launch chromium with the flag --remote-debugging-port=21222, you will know that the browserURL (not the browserWSEndpoint) will be http://127.0.0.1:21222
  • Jason Zhou
    Jason Zhou about 4 years
    I opened chromium with command chromium-browser --remote-debugging-port=21222. but when connect, I get an error Error: connect ECONNREFUSED 127.0.0.1:21222.
  • Taha Daboussi
    Taha Daboussi almost 3 years
    hi , i wonder if there is a way to automatically for puppeteer to find an available port and connect to it ?