问题
I want to connect to an existing (already opened) chrome browser with 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.
回答1:
The webSocketDebuggerUrl
value belongs to each individual tab.
This method needs to be scrapped 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
来源:https://stackoverflow.com/questions/57957890/connect-with-pyppeteer-to-existing-chrome