Update docs plus init (#2073)

Co-authored-by: Shuchang Zheng <wintonzheng0325@gmail.com>
This commit is contained in:
Suchintan
2025-04-03 00:46:57 -04:00
committed by GitHub
parent 816d0e34d1
commit ff57f9977c
11 changed files with 804 additions and 750 deletions

View File

@@ -2,6 +2,8 @@ from __future__ import annotations
import asyncio
import os
import socket
import subprocess
import time
import uuid
from datetime import datetime
@@ -9,6 +11,7 @@ from pathlib import Path
from typing import Any, Awaitable, Callable, Protocol
import aiofiles
import psutil
import structlog
from playwright.async_api import BrowserContext, ConsoleMessage, Download, Page, Playwright
from pydantic import BaseModel, PrivateAttr
@@ -303,6 +306,31 @@ def _get_cdp_port(kwargs: dict) -> int | None:
return None
def _is_port_in_use(port: int) -> bool:
"""Check if a port is already in use."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind(("localhost", port))
return False
except socket.error:
return True
def _is_chrome_running() -> bool:
"""Check if Chrome is already running."""
chrome_process_names = ["chrome", "google-chrome", "google chrome"]
for proc in psutil.process_iter(["name"]):
try:
proc_name = proc.info["name"].lower()
if proc_name == "chrome_crashpad_handler":
continue
if any(chrome_name in proc_name for chrome_name in chrome_process_names):
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
async def _create_headless_chromium(
playwright: Playwright, proxy_location: ProxyLocation | None = None, **kwargs: dict
) -> tuple[BrowserContext, BrowserArtifacts, BrowserCleanupFunc]:
@@ -352,6 +380,28 @@ async def _create_headful_chromium(
async def _create_cdp_connection_browser(
playwright: Playwright, proxy_location: ProxyLocation | None = None, **kwargs: dict
) -> tuple[BrowserContext, BrowserArtifacts, BrowserCleanupFunc]:
browser_type = settings.BROWSER_TYPE
browser_path = settings.CHROME_EXECUTABLE_PATH
if browser_type == "cdp-connect" and browser_path:
# First check if Chrome is already running
if _is_chrome_running():
raise Exception(
"Chrome is already running. Please close all Chrome instances before starting with remote debugging."
)
# Then check if the debugging port is already in use
if _is_port_in_use(9222):
raise Exception("Port 9222 is already in use. Another process may be using this port.")
browser_process = subprocess.Popen(
[browser_path, "--remote-debugging-port=9222"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
# Add small delay to allow browser to start
time.sleep(1)
if browser_process.poll() is not None:
raise Exception(f"Failed to open browser. browser_path: {browser_path}")
browser_args = BrowserContextFactory.build_browser_args()
browser_artifacts = BrowserContextFactory.build_browser_artifacts(