Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect with pyppeteer to existing chrome

I want to connect to an existing (already opened, by the user, without any extra flags) Chrome browser using pyppeteer so I would be able to control it.

I can do almost every manual action before (for example, enabling remote debugging mode in existing chrome), but it is preferable to do it with the least actions.

In order to use browser.connect, I need to give it browserWSEndpoint, which is equivalent to webSocketDebuggerUrl under 'http://localhost:9222/json/version'.

My problem is that I can get to 'http://localhost:9222/json/version' only when I run chrome with --headless tag, otherwise I can't get this string.

I tried running from cmd: chrome --disable-gpu --remote-debugging-port=9222 https://stackoverflow.com which opens a new tab under the opened chrome instance, but I still can't reach 'http://localhost:9222/json/version' to get webSocketDebuggerUrl (I get 'ERR_CONNECTION_REFUSED' when trying to reach that address).

How can I do it? I couldn't find anything on the net.

Edit (also refined the first paragraph of the question):

Thanks all for the answers, but it seems that what I originally wanted to do is not possible. You cannot connect to an existing Chrome if it wasn't first opened (the first instance of the browser) with the flag --remote-debugging-port=XXXX that allows you to remotely control it. As soon as a first instance of the browser was opened - it locks the user data of the browser and flags can't be added from the command line to the browser (only from inside the browser itself, by the user).

like image 669
Noam Avatar asked Nov 07 '22 13:11

Noam


1 Answers

The webSocketDebuggerUrl value belongs to each individual tab.
This method needs to be removed from your already open instance, which needs to be launched totally fresh using --remote-debugging-port=9222.

Try running this before starting up chrome.

taskkill /F /IM chrome.exe

now the url you want is http://127.0.0.1:9222/json and will look like this.
screenshotof :9222/json

If that solves it, great but I think what your actually wanting to do is launch your native chrome containing your personal data and have that instance accept commands from your scripts.

Luckily this is a far simpler goal!

You can accomplish that by passing executablePath and userDataDir to launch

from pyppeteer import launch
import asyncio

url = 'https://stackoverflow.com/questions/57957890/connect-with-pyppeteer-to-existing-chrome'

async def main():
    global browser
    browser = await launch(headless=False, executablePath='C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe', userDataDir="\\Local\\Google\\Chrome\\User Data")
    page = await browser.newPage()
    await page.goto(url)
    # await browser.close()

 run = asyncio.run(main())

One of the issues with this method, Is you will fail to open a new page if there are other existing chrome instances running while you create one.
I would suggest setting up a separate installation of chrome that you can setup how you want it, and than control with pyppeteer.
I'll update when If I find other bugs with this method.

Could have a script to update the userdata from Chrome proper whenever you launch it this way

like image 190
John Edens Avatar answered Nov 15 '22 07:11

John Edens