Connect with pyppeteer to existing chrome

2024/10/14 23:20:01

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).

Answer

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 asynciourl = 'https://stackoverflow.com/questions/57957890/connect-with-pyppeteer-to-existing-chrome'async def main():global browserbrowser = 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

https://en.xdnf.cn/q/69360.html

Related Q&A

Combining asyncio with a multi-worker ProcessPoolExecutor and for async

My question is very similar to Combining asyncio with a multi-worker ProcessPoolExecutor - however a slight change (I believe its the async for) makes the excellent answers there unusuable for me.I am …

Convert UTF-8 to string literals in Python

I have a string in UTF-8 format but not so sure how to convert this string to its corresponding character literal. For example I have the string:My string is: Entre\xc3\xa9Example one:This code:uEntre\…

Memory usage not getting lowered even after job is completed successfully

I have a job added in apscheduler which loads some data in memory and I am deleting all the objects after the job is complete. Now if I run this job with python it works successfully and memory drop af…

How to output sklearn standardscaler

I have standardized my data in sklearn using preprocessing.standardscaler. Question is how could I save this in my local for latter use?Thanks

How to use Jobqueue in Python-telegram-bot

I have able to make a bot very easily by reading the docs but Jobqueue is not working as per it is written. The run_daily method uses a datetime.time object to send the message at a particular time but…

Is there a way to override default assert in pytest (python)?

Id like to a log some information to a file/database every time assert is invoked. Is there a way to override assert or register some sort of callback function to do this, every time assert is invoked?…

How to install pycairo on osx?

I am trying to install the pycairo (Python bindings for the cairo graphics library) under OSX.I started witheasy_install pycairoand got: Requested cairo >= 1.8.8 but version of cairo is 1.0.4error: …

How do I change a value in a .npz file?

I want to change one value in an npz file.The npz file contains several npys, I want all but one ( run_param ) to remain unchanged and I want to save over the original file.This is my working code:DATA…

How to load *.hdr files using python

I would like to read an environment map in *.hdr file format. It seems that very popular libraries doesnt support .hdr file reading, for example, OpenCV, PIL etc.. So how to read a .hdr file into a num…

What is the preferred way to compose a set from multiple lists in Python

I have a few different lists that I want to turn into a set and use to find the difference from another set. Lets call them A, B, and C. Is the more optimal way to do this set(A + B + C) or set(A).unio…