Playwright无法成功连接本地Chrome并跳转页面的问题求助
Playwright无法成功连接本地Chrome并跳转页面的问题求助
大家好,我遇到了一个Playwright连接本地Chrome的问题,想请各位帮忙排查下:
我运行的代码
from playwright.sync_api import Playwright, sync_playwright, expect from playwright.async_api import async_playwright import asyncio import subprocess chrome_path = r'"C:\Program Files\Google\Chrome\Application\chrome.exe"' debugging_port = "--remote-debugging-port=9222" command = f"{chrome_path} {debugging_port}" subprocess.Popen(command, shell=True) async def main() -> None: async with async_playwright() as p: browser = await p.chromium.connect_over_cdp(("http://localhost:9222")) context = await browser.contexts[0] page = await context.new_page() await page.goto("https://www.google.com") print(await page.title()) if __name__ == '__main__': asyncio.run(main())
问题现象
运行这段代码后,本地Chrome确实会启动,但之后就没有任何反应了,完全没有按照预期跳转到https://www.google.com页面,之后命令行里弹出了错误日志。
收到的错误日志
PS C:\Code-Repo\Web-Crawler-Playwright> & C:/Users/anaconda3/envs/web-crawler-playwright/python.exe c:/Code-Repo/Web-Crawler-Playwright/tempt_chrome.py Traceback (most recent call last): File "c:/Code-Repo/Web-Crawler-Playwright/tempt_chrome.py", line 22, in <module> run(playwright) File "c:/Code-Repo/Web-Crawler-Playwright/tempt_chrome.py", line 12, in run browser = playwright.chromium.connect_over_cdp(("http://localhost:1234")) File "C:\Users\anaconda3\envs\web-crawler-playwright\lib\site-packages\playwright\sync_api\_generated.py", line 14816, in connect_over_cdp self._sync( File "C:\Users\anaconda3\envs\web-crawler-playwright\lib\site-packages\playwright\_impl\_sync_base.py", line 115, in _sync return task.result() File "C:\Users\anaconda3\envs\web-crawler-playwright\lib\site-packages\playwright\_impl\_browser_type.py", line 174, in connect_over_cdp response = await self._channel.send_return_as_dict("connectOverCDP", params) File "C:\Users\anaconda3\envs\web-crawler-playwright\lib\site-packages\playwright\_impl\_connection.py", line 64, in send_return_as_dict return await self._connection.wrap_api_call( File "C:\Users\anaconda3\envs\web-crawler-playwright\lib\site-packages\playwright\_impl\_connection.py", line 520, in wrap_api_call raise rewrite_error(error, f"{parsed_st['apiName']}: {error}") from None playwright._impl._errors.Error: BrowserType.connect_over_cdp: connect ECONNREFUSED ::1:1234 Call log: <ws preparing> retrieving websocket url from http://localhost:1234
我已经尝试过的解决方法
但这些操作都没能解决问题:
- 把
http://localhost:9222替换成http://127.0.0.1:9222 - 关闭了所有VPN
- 重启Chrome进程
我的需求是:希望Playwright能自动打开本地Chrome,然后成功跳转到https://www.google.com页面。
备注:内容来源于stack exchange,提问作者HorusLiang
相关产品推荐
相关产品推荐

