71 lines
2.1 KiB
Markdown
71 lines
2.1 KiB
Markdown
# Chrome CDP Launch Procedure
|
|
|
|
## Chrome 151+ Breaking Change
|
|
|
|
Chrome 151+ requires `--remote-allow-origins=*` for WebSocket connections to CDP.
|
|
Without it: `Handshake status 403 Forbidden - Rejected an incoming WebSocket connection`.
|
|
|
|
## Full Launch Command (Windows)
|
|
|
|
```bash
|
|
# Kill existing Chrome instances
|
|
taskkill /F /IM chrome.exe 2>/dev/null
|
|
|
|
# Launch with CDP enabled
|
|
"C:\Program Files\Google\Chrome\Application\chrome.exe" \
|
|
--remote-debugging-port=9222 \
|
|
--remote-allow-origins=* \
|
|
--user-data-dir=%USERPROFILE%/.chrome-cdp-profile \
|
|
--no-first-run \
|
|
--no-default-browser-check
|
|
```
|
|
|
|
## Verify CDP is alive
|
|
|
|
```bash
|
|
curl -s http://127.0.0.1:9222/json/version
|
|
# Expected: {"Browser": "Chrome/151.0.7922.109", ...}
|
|
```
|
|
|
|
## Python: kill + launch
|
|
|
|
```python
|
|
import subprocess, time, os
|
|
|
|
subprocess.run(['taskkill', '/F', '/IM', 'chrome.exe'], capture_output=True)
|
|
time.sleep(2)
|
|
|
|
user_data = os.path.expanduser("~/.chrome-cdp-profile")
|
|
os.makedirs(user_data, exist_ok=True)
|
|
|
|
subprocess.Popen([
|
|
r"C:\Program Files\Google\Chrome\Application\chrome.exe",
|
|
f'--user-data-dir={user_data}',
|
|
'--remote-debugging-port=9222',
|
|
'--remote-allow-origins=*',
|
|
'--no-first-run',
|
|
'--no-default-browser-check',
|
|
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
```
|
|
|
|
## Python: health check
|
|
|
|
```python
|
|
import urllib.request, json
|
|
|
|
try:
|
|
resp = urllib.request.urlopen("http://127.0.0.1:9222/json/version", timeout=3)
|
|
data = json.loads(resp.read())
|
|
print(f"CDP OK: {data.get('Browser', 'unknown')}")
|
|
except Exception as e:
|
|
print(f"CDP DEAD: {e}")
|
|
```
|
|
|
|
## Common Errors
|
|
|
|
| Error | Cause | Fix |
|
|
|-------|-------|-----|
|
|
| `403 Forbidden` on WebSocket | Chrome 151+ without `--remote-allow-origins` | Add `--remote-allow-origins=*` |
|
|
| `Connection refused` on :9222 | Chrome not launched with `--remote-debugging-port` | Kill all Chrome, relaunch with flag |
|
|
| `user-data-dir is already in use` | Another Chrome instance using same profile | Use unique `--user-data-dir` path |
|
|
| `taskkill` not finding Chrome | Chrome running as background process | Check with `tasklist /FI "IMAGENAME eq chrome.exe"` | |