playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Bug]: playwright._impl._errors.Error: BrowserType.connect_over_cdp: WebSocket error: connect ECONNREFUSED

Open Louis24 opened this issue 1 year ago • 1 comments

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

Louis24 avatar Jun 27 '24 03:06 Louis24

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.

pavelfeldman avatar Jun 27 '24 15:06 pavelfeldman

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?

mxschmitt avatar Jul 01 '24 08:07 mxschmitt

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

image when I change to browser = playwright.chromium.connect_over_cdp("ws://127.0.0.1:1234")

image

Louis24 avatar Jul 02 '24 14:07 Louis24

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?

Louis24 avatar Jul 02 '24 14:07 Louis24

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)

mxschmitt avatar Jul 02 '24 15:07 mxschmitt

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 image

Which I expect like this image

is it possible to open a broswer with cache and plugin? and how about codegen using my own broswer?

Louis24 avatar Jul 03 '24 00:07 Louis24

This doesn't look like and end-to-end testing use-case, so I'm going to close it for now. If you want to open an existing profile, see e.g. here or here.

mxschmitt avatar Jul 03 '24 09:07 mxschmitt