|
15 | 15 | import os |
16 | 16 | import sys |
17 | 17 | import time |
| 18 | +from urllib.parse import urlparse |
18 | 19 |
|
19 | 20 | from playwright.async_api import async_playwright |
20 | 21 |
|
@@ -126,9 +127,54 @@ async def main(): |
126 | 127 |
|
127 | 128 | print(f"Browser endpoint URL: {endpoint_url}") |
128 | 129 |
|
129 | | - # Connect with Playwright and set cookies |
| 130 | + # Parse endpoint URL to check connectivity |
| 131 | + parsed_url = urlparse(endpoint_url) |
| 132 | + if parsed_url.hostname: |
| 133 | + print(f"Endpoint hostname: {parsed_url.hostname}, port: {parsed_url.port or 443}") |
| 134 | + |
| 135 | + # Wait a bit for browser to be fully ready |
| 136 | + print("Waiting for browser to be fully ready...") |
| 137 | + await asyncio.sleep(5) # Increased wait time |
| 138 | + |
| 139 | + # Connect with Playwright and set cookies with retry mechanism |
| 140 | + max_retries = 5 # Increased retries |
| 141 | + retry_delay = 5 |
| 142 | + browser = None |
| 143 | + |
130 | 144 | async with async_playwright() as p: |
131 | | - browser = await p.chromium.connect_over_cdp(endpoint_url) |
| 145 | + for attempt in range(max_retries): |
| 146 | + try: |
| 147 | + print(f"Attempting to connect to browser (attempt {attempt + 1}/{max_retries})...") |
| 148 | + print(f" Endpoint: {endpoint_url[:100]}...") # Print first 100 chars |
| 149 | + |
| 150 | + # Try connecting with increased timeout |
| 151 | + browser = await p.chromium.connect_over_cdp( |
| 152 | + endpoint_url, |
| 153 | + timeout=90000 # Increase timeout to 90 seconds |
| 154 | + ) |
| 155 | + print("Successfully connected to browser") |
| 156 | + break |
| 157 | + except Exception as e: |
| 158 | + error_msg = str(e) |
| 159 | + print(f"Connection attempt {attempt + 1} failed: {error_msg}") |
| 160 | + print(f" Error type: {type(e).__name__}") |
| 161 | + |
| 162 | + # If it's an SSL or network error, wait longer before retry |
| 163 | + if "EBADF" in error_msg or "SSL" in error_msg or "certificate" in error_msg.lower(): |
| 164 | + print(" Detected SSL/network error, will wait longer before retry") |
| 165 | + retry_delay = 10 |
| 166 | + |
| 167 | + if attempt < max_retries - 1: |
| 168 | + print(f"Retrying in {retry_delay} seconds...") |
| 169 | + await asyncio.sleep(retry_delay) |
| 170 | + # Increase wait time for subsequent retries |
| 171 | + retry_delay = min(retry_delay + 5, 20) |
| 172 | + else: |
| 173 | + print(f"All connection attempts failed. Last error: {error_msg}") |
| 174 | + raise |
| 175 | + |
| 176 | + if browser is None: |
| 177 | + raise RuntimeError("Failed to connect to browser after all retries") |
132 | 178 | context_p = browser.contexts[0] if browser.contexts else await browser.new_context() |
133 | 179 | page = await context_p.new_page() |
134 | 180 |
|
@@ -311,9 +357,54 @@ async def main(): |
311 | 357 |
|
312 | 358 | print(f"Second session browser endpoint URL: {endpoint_url2}") |
313 | 359 |
|
314 | | - # Check cookies in second session |
| 360 | + # Parse endpoint URL to check connectivity |
| 361 | + parsed_url2 = urlparse(endpoint_url2) |
| 362 | + if parsed_url2.hostname: |
| 363 | + print(f"Second session endpoint hostname: {parsed_url2.hostname}, port: {parsed_url2.port or 443}") |
| 364 | + |
| 365 | + # Wait a bit for browser to be fully ready |
| 366 | + print("Waiting for second session browser to be fully ready...") |
| 367 | + await asyncio.sleep(5) # Increased wait time |
| 368 | + |
| 369 | + # Check cookies in second session with retry mechanism |
| 370 | + max_retries = 5 # Increased retries |
| 371 | + retry_delay = 5 |
| 372 | + browser2 = None |
| 373 | + |
315 | 374 | async with async_playwright() as p2: |
316 | | - browser2 = await p2.chromium.connect_over_cdp(endpoint_url2) |
| 375 | + for attempt in range(max_retries): |
| 376 | + try: |
| 377 | + print(f"Attempting to connect to second session browser (attempt {attempt + 1}/{max_retries})...") |
| 378 | + print(f" Endpoint: {endpoint_url2[:100]}...") # Print first 100 chars |
| 379 | + |
| 380 | + # Try connecting with increased timeout |
| 381 | + browser2 = await p2.chromium.connect_over_cdp( |
| 382 | + endpoint_url2, |
| 383 | + timeout=90000 # Increase timeout to 90 seconds |
| 384 | + ) |
| 385 | + print("Successfully connected to second session browser") |
| 386 | + break |
| 387 | + except Exception as e: |
| 388 | + error_msg = str(e) |
| 389 | + print(f"Connection attempt {attempt + 1} failed: {error_msg}") |
| 390 | + print(f" Error type: {type(e).__name__}") |
| 391 | + |
| 392 | + # If it's an SSL or network error, wait longer before retry |
| 393 | + if "EBADF" in error_msg or "SSL" in error_msg or "certificate" in error_msg.lower(): |
| 394 | + print(" Detected SSL/network error, will wait longer before retry") |
| 395 | + retry_delay = 10 |
| 396 | + |
| 397 | + if attempt < max_retries - 1: |
| 398 | + print(f"Retrying in {retry_delay} seconds...") |
| 399 | + await asyncio.sleep(retry_delay) |
| 400 | + # Increase wait time for subsequent retries |
| 401 | + retry_delay = min(retry_delay + 5, 20) |
| 402 | + else: |
| 403 | + print(f"All connection attempts failed. Last error: {error_msg}") |
| 404 | + raise |
| 405 | + |
| 406 | + if browser2 is None: |
| 407 | + raise RuntimeError("Failed to connect to second session browser after all retries") |
317 | 408 | context2 = ( |
318 | 409 | browser2.contexts[0] |
319 | 410 | if browser2.contexts |
|
0 commit comments