使用 pyppeteer 连接到现有的 chrome [英] Connect with pyppeteer to existing chrome

查看:130
本文介绍了使用 pyppeteer 连接到现有的 chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 pyppeteer 这样我就可以控制它了.

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.

我之前几乎可以执行所有手动操作(例如,在现有 chrome 中启用远程调试模式),但最好使用最少的操作来完成.

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.

为了使用browser.connect,我需要给它browserWSEndpoint,相当于'http://下的webSocketDebuggerUrl本地主机:9222/json/version'.

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

我的问题是,只有当我使用 --headless 标签运行 chrome 时,我才能到达 '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.

我尝试从 cmd 运行:chrome --disable-gpu --remote-debugging-port=9222 https://stackoverflow.com它在打开的 chrome 实例下打开一个新选项卡,但我仍然无法访问 'http://localhost:9222/json/version' 来获取 webSocketDebuggerUrl(我在尝试时得到 'ERR_CONNECTION_REFUSED'到达那个地址).

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.

谢谢大家的解答,但是看来我原本想做的事情是不可能的了.如果现有 Chrome 不是首次打开(浏览器的第一个实例),并且带有标志 --remote-debugging-port=XXXX 允许你来远程控制它.一旦打开浏览器的第一个实例 - 它就会锁定浏览器的用户数据,并且标志不能从命令行添加到浏览器(只能从浏览器内部,由用户).

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

推荐答案

webSocketDebuggerUrl 值属于每个单独的选项卡.
此方法需要从您已经打开的实例中废弃,需要使用 --remote-debugging-port=9222 全新启动.

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.

在启动 chrome 之前尝试运行它.

Try running this before starting up chrome.

taskkill/F/IM chrome.exe

现在你想要的 url 是 http://127.0.0.1:9222/json 并且会看起来像这样.
截图:9222/json

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

如果这能解决问题,那很好,但我认为您真正想做的是启动包含您个人数据的本机 chrome,并让该实例接受来自您脚本的命令.

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!

您可以通过传递 executablePath 和 userDataDir 来启动

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

这种方法的一个问题是,如果在您创建一个现有的 chrome 实例时,您将无法打开一个新页面.
我建议设置一个单独的 chrome 安装,你可以设置你想要的方式,而不是用 pyppeteer 控制.
如果我发现此方法的其他错误,我会更新.

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.

每当您以这种方式启动它时,都可以有一个脚本来更新 Chrome 中的用户数据

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

这篇关于使用 pyppeteer 连接到现有的 chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆