|
7 | 7 | import contextlib |
8 | 8 | import io |
9 | 9 | import re |
| 10 | +import shutil |
10 | 11 |
|
11 | 12 | # Moodle 主站(需要在第一次輸入帳密前就可用) |
12 | 13 | MOODLE_BASE_URL = "https://elearningv4.nuk.edu.tw" |
@@ -381,42 +382,7 @@ def _create_chrome_driver(options_to_use=None): |
381 | 382 | print(f"[WARN] Chrome 啟動失敗,改用 Safari。原因: {e}") |
382 | 383 | return webdriver.Safari() |
383 | 384 |
|
384 | | - # Windows 偶發 DevToolsActivePort 問題時,自動換 profile 重試一次。 |
385 | | - if os.name == 'nt': |
386 | | - max_retries = 3 |
387 | | - for attempt in range(max_retries): |
388 | | - try: |
389 | | - if attempt == 0: |
390 | | - return _create_chrome_driver() |
391 | | - |
392 | | - # 重試:一律使用獨立臨時 profile,避免與使用者已開啟的 Chrome 衝突 |
393 | | - retry_options = Options() |
394 | | - if chrome_options is not None: |
395 | | - for arg in chrome_options.arguments: |
396 | | - if not arg.startswith("--user-data-dir="): |
397 | | - retry_options.add_argument(arg) |
398 | | - retry_options.page_load_strategy = chrome_options.page_load_strategy |
399 | | - if hasattr(chrome_options, 'binary_location'): |
400 | | - retry_options.binary_location = chrome_options.binary_location |
401 | | - for key, value in chrome_options.experimental_options.items(): |
402 | | - retry_options.add_experimental_option(key, value) |
403 | | - |
404 | | - _prepare_windows_chrome_options(retry_options) |
405 | | - fallback_dir = tempfile.mkdtemp(prefix="chrome_profile_", dir=BASE_DOWNLOAD_DIR) |
406 | | - retry_options.add_argument(f"--user-data-dir={fallback_dir}") |
407 | | - print(f"[RETRY {attempt}/{max_retries - 1}] 使用臨時 Chrome 配置檔: {fallback_dir}") |
408 | | - return _create_chrome_driver(retry_options) |
409 | | - except WebDriverException as e: |
410 | | - if _is_chrome_startup_issue(e): |
411 | | - # 不再 taskkill 使用者 Chrome;改用上方獨立 profile 重試 |
412 | | - if attempt < max_retries - 1: |
413 | | - time.sleep(1) |
414 | | - continue |
415 | | - print(f"{RED}X Chrome 啟動失敗已達最大重試次數{RESET}") |
416 | | - raise |
417 | | - else: |
418 | | - raise |
419 | | - |
| 385 | + # Windows:不再使用「臨時 profile 重試」邏輯(避免產生額外配置檔/行為不一致)。 |
420 | 386 | return _create_chrome_driver() |
421 | 387 |
|
422 | 388 | # 嘗試導入 RAR 解壓工具 |
@@ -4615,24 +4581,7 @@ def cleanup_driver(): |
4615 | 4581 | try: |
4616 | 4582 | driver = create_webdriver(chrome_options_visible, hide_windows_console=True) |
4617 | 4583 | except WebDriverException as e: |
4618 | | - # Windows 偶發 DevToolsActivePort 啟動失敗:改用臨時 profile 再重試 |
4619 | | - if os.name == 'nt': |
4620 | | - time.sleep(1) |
4621 | | - chrome_options_visible_retry = Options() |
4622 | | - chrome_options_visible_retry.add_argument("--log-level=3") |
4623 | | - chrome_options_visible_retry.add_experimental_option("excludeSwitches", ["enable-logging"]) |
4624 | | - chrome_options_visible_retry.add_argument("--disable-gpu") |
4625 | | - chrome_options_visible_retry.add_argument("--disable-dev-shm-usage") |
4626 | | - chrome_options_visible_retry.add_argument("--remote-debugging-pipe") |
4627 | | - chrome_options_visible_retry.add_argument("--disable-software-rasterizer") |
4628 | | - chrome_options_visible_retry.add_argument("--no-sandbox") |
4629 | | - chrome_options_visible_retry.add_argument("--disable-features=RendererCodeIntegrity") |
4630 | | - submit_fallback_dir = tempfile.mkdtemp(prefix="chrome_profile_submit_fallback_", dir=BASE_DOWNLOAD_DIR) |
4631 | | - chrome_options_visible_retry.add_argument(f"--user-data-dir={submit_fallback_dir}") |
4632 | | - apply_chrome_binary_option(chrome_options_visible_retry) |
4633 | | - driver = create_webdriver(chrome_options_visible_retry, hide_windows_console=True) |
4634 | | - else: |
4635 | | - raise |
| 4584 | + raise |
4636 | 4585 |
|
4637 | 4586 | # 重新登入 |
4638 | 4587 | if not ensure_logged_in(driver, USERNAME, PASSWORD, silent=False, max_retries=2): |
|
0 commit comments