[Bug]: playwright._impl._errors.Error: BrowserType.connect_over_cdp: WebSocket error: connect ECONNREFUSED
Version
1.43
Steps to reproduce
import subprocess
from playwright.sync_api import Playwright, sync_playwright
chrome_path = r'"C:\Program Files\Google\Chrome\Application\chrome.exe"'
debugging_port = "--remote-debugging-port=1234"
command = f"{chrome_path} {debugging_port}"
subprocess.Popen(command, shell=True)
def run(playwright: Playwright) -> None:
browser = playwright.chromium.connect_over_cdp(("http://localhost:1234"))
context = browser.contexts[0]
page = context.new_page()
page.goto("https://www.youtube.com/")
page.wait_for_selector('yt-icon-button#button.style-scope.ytd-topbar-menu-button-renderer').click()
"""
"""
with sync_playwright() as playwright:
run(playwright)
Expected behavior
open the browser in the path chrome_path = r'"C:\Program Files\Google\Chrome\Application\chrome.exe"'
Actual behavior
raise rewrite_error(error, f"{parsed_st['apiName']}: {error}") from None
playwright._impl._errors.Error: BrowserType.connect_over_cdp: Unexpected status 400 when connecting to http://localhost:1234/json/version/.
This does not look like a DevTools server, try connecting via ws://.
Call log:
<ws preparing> retrieving websocket url from http://localhost:1234
Additional context
if I change code to browser = playwright.chromium.connect_over_cdp(("ws://localhost:1234")) got this error
playwright._impl._errors.Error: BrowserType.connect_over_cdp: WebSocket error: connect ECONNREFUSED ::1:1234
Call log:
<ws connecting> ws://localhost:1234/
- <ws error> ws://localhost:1234/ error connect ECONNREFUSED ::1:1234
- <ws connect error> ws://localhost:1234/ connect ECONNREFUSED ::1:1234
- <ws disconnected> ws://localhost:1234/ code=1006 reason=
Environment
System:
os: win11
@playwright/test: Version: 1.43.0
Try replacing
playwright.chromium.connect_over_cdp(("http://localhost:1234"))
with
playwright.chromium.connect_over_cdp(("http://127.0.0.1:1234"))
It sound like localhost resolves to ipv6 on your system.
My guess is that
chrome_path = r'"C:\Program Files\Google\Chrome\Application\chrome.exe"' debugging_port = "--remote-debugging-port=1234"
command = f"{chrome_path} {debugging_port}" subprocess.Popen(command, shell=True)
is not working or not waiting long enough to Playwright to connect, so the server is not listening yet. Could you verify that?
Try replacing
playwright.chromium.connect_over_cdp(("http://localhost:1234"))with
playwright.chromium.connect_over_cdp(("http://127.0.0.1:1234"))It sound like localhost resolves to ipv6 on your system.
Try replacing
playwright.chromium.connect_over_cdp(("http://localhost:1234"))with
playwright.chromium.connect_over_cdp(("http://127.0.0.1:1234"))It sound like localhost resolves to ipv6 on your system.
browser = playwright.chromium.connect_over_cdp("http://127.0.0.1:1234")
when I change to browser = playwright.chromium.connect_over_cdp("ws://127.0.0.1:1234")
My guess is that
chrome_path = r'"C:\Program Files\Google\Chrome\Application\chrome.exe"' debugging_port = "--remote-debugging-port=1234"
command = f"{chrome_path} {debugging_port}" subprocess.Popen(command, shell=True)
is not working or not waiting long enough to Playwright to connect, so the server is not listening yet. Could you verify that?
sure, i'll try wait longer time. I have setting the time=10s, still error. I guess a possible reason is that I use a vpn, is that cause the error?
I had to modify it a little bit, since I'm on macOS, then it was working for me, note the --user-data-dir:
chrome_path = r'"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"'
debugging_port = "--remote-debugging-port=1234 --user-data-dir=foo"
command = f"{chrome_path} {debugging_port}"
subprocess.Popen(command, shell=True)
I had to modify it a little bit, since I'm on macOS, then it was working for me, note the
--user-data-dir:chrome_path = r'"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"' debugging_port = "--remote-debugging-port=1234 --user-data-dir=foo" command = f"{chrome_path} {debugging_port}" subprocess.Popen(command, shell=True)
thx, not working on windows.
Since YouTube has a security check mechanism and detected that the Playwright framework contained risks, I wondered if there was a possibility to let Playwright open the local browser and load local cookies and plug-ins, which would save the login step.
So I tried another method:
def run(playwright): browser = playwright.chromium.launch_persistent_context( user_data_dir=r"C:\Users\Louis\AppData\Local\Google\Chrome\User Data\Profile 1", # 设置用户数据目录 executable_path=r'C:\Program Files\Google\Chrome\Application\chrome.exe', # Chrome 可执行文件路径 accept_downloads=True, # 允许下载 headless=False, # 非无头模式,即显示浏览器界面 bypass_csp=True, # 绕过 Content Security Policy slow_mo=10, # 减速执行,便于调试 args=[ '--disable-blink-features=AutomationControlled', # 禁用 Blink 功能控制 '--remote-debugging-port=1234', # 启用远程调试端口 '--in-process-plugins', # 插件在浏览器进程中运行 '--allow-outdated-plugins', # 允许使用过期插件 ] ) page = browser.new_page() page.goto("https://www.baidu.com/") print(page.title()) page.pause()
with sync_playwright() as playwright: run(playwright)
The results is like this
Which I expect like this
is it possible to open a broswer with cache and plugin? and how about codegen using my own broswer?