• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kimata / my-py-lib / 23675507534

28 Mar 2026 02:29AM UTC coverage: 62.067% (+0.01%) from 62.056%
23675507534

push

github

kimata
fix(selenium): Emulation.setLocaleOverride を削除して Preferences 肥大化を防止

Chrome 142 + 日本語ロケールの組み合わせで、内蔵拡張機能の manifest.name が
Preferences 保存のたびに UTF-8 多重エンコードされ、ファイルが際限なく肥大化する
バグを回避する。--lang=ja-JP と Accept-Language ヘッダーで日本語環境は維持される。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

3652 of 5884 relevant lines covered (62.07%)

0.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

35.3
/src/my_lib/selenium_util.py
1
#!/usr/bin/env python3
2
"""
3
Selenium を Chrome Driver を使って動かします。
4

5
Usage:
6
  selenium_util.py [-c CONFIG] [-D]
7

8
Options:
9
  -c CONFIG         : CONFIG を設定ファイルとして読み込んで実行します。
10
                      [default: tests/fixtures/config.example.yaml]
11
  -D                : デバッグモードで動作します。
12
"""
13

14
from __future__ import annotations
1✔
15

16
import datetime
1✔
17
import inspect
1✔
18
import io
1✔
19
import logging
1✔
20
import os
1✔
21
import pathlib
1✔
22
import random
1✔
23
import re
1✔
24
import shutil
1✔
25
import signal
1✔
26
import subprocess
1✔
27
import time
1✔
28
from typing import TYPE_CHECKING, Any, Self, TypeVar
1✔
29

30
import PIL.Image
1✔
31
import psutil
1✔
32
import selenium
1✔
33
import selenium.common.exceptions
1✔
34
import selenium.webdriver.chrome.options
1✔
35
import selenium.webdriver.chrome.service
1✔
36
import selenium.webdriver.common.action_chains
1✔
37
import selenium.webdriver.common.by
1✔
38
import selenium.webdriver.common.keys
1✔
39
import selenium.webdriver.support.expected_conditions
1✔
40

41
import my_lib.chrome_util
1✔
42
import my_lib.time
1✔
43

44
if TYPE_CHECKING:
45
    import types
46
    from collections.abc import Callable
47

48
    from selenium.webdriver.remote.webdriver import WebDriver
49
    from selenium.webdriver.support.wait import WebDriverWait
50

51
T = TypeVar("T")
1✔
52

53
WAIT_RETRY_COUNT: int = 1
1✔
54
LOG_MAX_BYTES: int = 10 * 1024 * 1024  # 10MB
1✔
55
LOG_BACKUP_COUNT: int = 3
1✔
56

57

58
class SeleniumError(Exception):
1✔
59
    """Selenium 関連エラーの基底クラス"""
60

61

62
def _get_rotated_path(log_file: pathlib.Path, index: int) -> pathlib.Path:
1✔
63
    """ローテーションされたログファイルのパスを生成"""
64
    return log_file.parent / f"{log_file.name}.{index}"
×
65

66

67
def _rotate_log_file(
1✔
68
    log_file: pathlib.Path,
69
    max_bytes: int = LOG_MAX_BYTES,
70
    backup_count: int = LOG_BACKUP_COUNT,
71
) -> bool:
72
    """ログファイルをローテーションする(copytruncate 方式)
73

74
    Chrome がファイルを開いたまま書き込みを続けるため、
75
    ファイルをコピーしてからトランケートする方式を採用。
76

77
    Args:
78
        log_file: ローテーション対象のログファイルパス
79
        max_bytes: ローテーションを実行するファイルサイズ閾値(バイト)
80
        backup_count: 保持するバックアップ世代数
81

82
    Returns:
83
        ローテーションを実行した場合は True
84

85
    """
86
    if not log_file.exists():
×
87
        return False
×
88

89
    try:
×
90
        file_size = log_file.stat().st_size
×
91
    except OSError:
×
92
        return False
×
93

94
    if file_size <= max_bytes:
×
95
        return False
×
96

97
    logging.info(
×
98
        "Rotating log file: %s (size: %.1f MB)",
99
        log_file.name,
100
        file_size / (1024 * 1024),
101
    )
102

103
    # 最古の世代を削除し、世代をシフト
104
    for i in range(backup_count, 0, -1):
×
105
        rotated = _get_rotated_path(log_file, i)
×
106
        if i == backup_count:
×
107
            rotated.unlink(missing_ok=True)
×
108
        elif rotated.exists():
×
109
            rotated.rename(_get_rotated_path(log_file, i + 1))
×
110

111
    # 現在のファイルを .1 にコピーしてからトランケート
112
    shutil.copy2(log_file, _get_rotated_path(log_file, 1))
×
113
    log_file.open("w").close()
×
114

115
    return True
×
116

117

118
def rotate_selenium_logs(
1✔
119
    log_path: pathlib.Path,
120
    max_bytes: int = LOG_MAX_BYTES,
121
    backup_count: int = LOG_BACKUP_COUNT,
122
) -> None:
123
    """Selenium 関連のログファイルをローテーションする
124

125
    Args:
126
        log_path: ログディレクトリのパス
127
        max_bytes: ローテーションを実行するファイルサイズ閾値(バイト、デフォルト: 10MB)
128
        backup_count: 保持するバックアップ世代数(デフォルト: 3)
129

130
    """
131
    if not log_path.exists():
×
132
        return
×
133

134
    for log_file in log_path.glob("*.log"):
×
135
        _rotate_log_file(log_file, max_bytes, backup_count)
×
136

137

138
def _get_chrome_version() -> int | None:
1✔
139
    try:
1✔
140
        result = subprocess.run(
1✔
141
            ["google-chrome", "--version"],  # noqa: S607
142
            capture_output=True,
143
            text=True,
144
            timeout=10,
145
            check=False,
146
        )
147
        match = re.search(r"(\d+)\.", result.stdout)
1✔
148
        if match:
1✔
149
            return int(match.group(1))
1✔
150
    except Exception:
1✔
151
        logging.warning("Failed to detect Chrome version")
1✔
152
    return None
1✔
153

154

155
def _create_chrome_options(
1✔
156
    profile_name: str,
157
    chrome_data_path: pathlib.Path,
158
    log_path: pathlib.Path,
159
    is_headless: bool,
160
) -> selenium.webdriver.chrome.options.Options:
161
    """Chrome オプションを作成する
162

163
    Args:
164
        profile_name: プロファイル名
165
        chrome_data_path: Chrome データディレクトリのパス
166
        log_path: ログディレクトリのパス
167
        is_headless: ヘッドレスモードで起動するか
168

169
    Returns:
170
        設定済みの Chrome オプション
171

172
    """
173
    options = selenium.webdriver.chrome.options.Options()
×
174

175
    if is_headless:
×
176
        options.add_argument("--headless=new")
×
177

178
    options.add_argument("--no-sandbox")  # for Docker
×
179
    options.add_argument("--disable-dev-shm-usage")  # for Docker
×
180
    options.add_argument("--disable-gpu")
×
181

182
    options.add_argument("--disable-popup-blocking")
×
183
    options.add_argument("--disable-plugins")
×
184

185
    options.add_argument("--no-first-run")
×
186

187
    options.add_argument("--lang=ja-JP")
×
188
    options.add_argument("--window-size=1920,1080")
×
189

190
    # NOTE: Accept-Language ヘッダーを日本語優先に設定
191
    options.add_experimental_option("prefs", {"intl.accept_languages": "ja-JP,ja,en-US,en"})
×
192

193
    options.add_argument("--user-data-dir=" + str(chrome_data_path / profile_name))
×
194

195
    options.add_argument("--enable-logging")
×
196
    options.add_argument("--v=1")
×
197

198
    chrome_log_file = log_path / f"chrome_{profile_name}.log"
×
199
    options.add_argument(f"--log-file={chrome_log_file!s}")
×
200

201
    if not is_headless:
×
202
        options.add_argument("--auto-open-devtools-for-tabs")
×
203

204
    return options
×
205

206

207
def _create_driver_impl(
1✔
208
    profile_name: str,
209
    data_path: pathlib.Path,
210
    is_headless: bool,
211
    use_subprocess: bool,
212
    use_undetected: bool,
213
    stealth_mode: bool,
214
) -> WebDriver:
215
    """WebDriver を作成する内部実装
216

217
    Args:
218
        profile_name: プロファイル名
219
        data_path: データディレクトリのパス
220
        is_headless: ヘッドレスモードで起動するか
221
        use_subprocess: サブプロセスで Chrome を起動するか(undetected_chromedriver のみ)
222
        use_undetected: undetected_chromedriver を使用するか
223
        stealth_mode: ボット検出回避のための User-Agent 偽装を行うか
224

225
    Returns:
226
        WebDriver インスタンス
227

228
    """
229
    chrome_data_path = data_path / "chrome"
×
230
    log_path = data_path / "log"
×
231

232
    # NOTE: Pytest を並列実行できるようにする
233
    suffix = os.environ.get("PYTEST_XDIST_WORKER", None)
×
234
    if suffix is not None:
×
235
        profile_name += "." + suffix
×
236

237
    chrome_data_path.mkdir(parents=True, exist_ok=True)
×
238
    log_path.mkdir(parents=True, exist_ok=True)
×
239

240
    rotate_selenium_logs(log_path)
×
241

242
    options = _create_chrome_options(profile_name, chrome_data_path, log_path, is_headless)
×
243

244
    service = selenium.webdriver.chrome.service.Service(
×
245
        service_args=["--verbose", f"--log-path={str(log_path / 'webdriver.log')!s}"],
246
    )
247

248
    if use_undetected:
×
249
        import undetected_chromedriver
×
250

251
        chrome_version = _get_chrome_version()
×
252

253
        # NOTE: user_multi_procs=True は既存の chromedriver ファイルが存在することを前提としているため、
254
        # ファイルが存在しない場合(CI環境の初回実行など)は False にする
255
        uc_data_path = pathlib.Path("~/.local/share/undetected_chromedriver").expanduser()
×
256
        use_multi_procs = uc_data_path.exists() and any(uc_data_path.glob("*chromedriver*"))
×
257

258
        driver = undetected_chromedriver.Chrome(
×
259
            service=service,
260
            options=options,
261
            use_subprocess=use_subprocess,
262
            version_main=chrome_version,
263
            user_multi_procs=use_multi_procs,
264
        )
265
    else:
266
        driver = selenium.webdriver.Chrome(
×
267
            service=service,
268
            options=options,
269
        )
270

271
    driver.set_page_load_timeout(30)
×
272

273
    # CDP を使って日本語ロケールを強制設定
274
    set_japanese_locale(driver)
×
275

276
    # ボット検出回避のための設定を適用(オプション)
277
    if stealth_mode:
×
278
        set_stealth_mode(driver)
×
279

280
    return driver
×
281

282

283
def create_driver(
1✔
284
    profile_name: str,
285
    data_path: pathlib.Path,
286
    is_headless: bool = True,
287
    clean_profile: bool = False,
288
    auto_recover: bool = True,
289
    use_undetected: bool = True,
290
    use_subprocess: bool = False,
291
    stealth_mode: bool = True,
292
) -> WebDriver:
293
    """Chrome WebDriver を作成する
294

295
    Args:
296
        profile_name: プロファイル名
297
        data_path: データディレクトリのパス
298
        is_headless: ヘッドレスモードで起動するか
299
        clean_profile: 起動前にロックファイルを削除するか
300
        auto_recover: プロファイル破損時に自動リカバリするか
301
        use_undetected: undetected_chromedriver を使用するか(デフォルト: True)
302
        use_subprocess: サブプロセスで Chrome を起動するか(undetected_chromedriver のみ)
303
        stealth_mode: ボット検出回避のための User-Agent 偽装を行うか(デフォルト: True)
304

305
    Raises:
306
        ValueError: use_undetected=False かつ use_subprocess=True の場合
307

308
    """
309
    if not use_undetected and use_subprocess:
×
310
        raise ValueError("use_subprocess=True は use_undetected=True の場合のみ有効です")
×
311

312
    # NOTE: ルートロガーの出力レベルを変更した場合でも Selenium 関係は抑制する
313
    logging.getLogger("urllib3.connectionpool").setLevel(logging.WARNING)
×
314
    logging.getLogger("selenium.webdriver.common.selenium_manager").setLevel(logging.WARNING)
×
315
    logging.getLogger("selenium.webdriver.remote.remote_connection").setLevel(logging.WARNING)
×
316

317
    # NOTE: chrome_util の内部関数を使用(同一パッケージ内での使用は許容)
318
    actual_profile_name = my_lib.chrome_util._get_actual_profile_name(profile_name)
×
319
    profile_path = data_path / "chrome" / actual_profile_name
×
320

321
    # プロファイル健全性チェック
322
    health = my_lib.chrome_util._check_profile_health(profile_path)
×
323
    if not health.is_healthy:
×
324
        logging.warning("Profile health check failed: %s", ", ".join(health.errors))
×
325

326
        if health.has_lock_files and not (health.has_corrupted_json or health.has_corrupted_db):
×
327
            # ロックファイルのみの問題なら削除して続行
328
            logging.info("Cleaning up lock files only")
×
329
            my_lib.chrome_util._cleanup_profile_lock(profile_path)
×
330
        elif auto_recover and (health.has_corrupted_json or health.has_corrupted_db):
×
331
            # JSON または DB が破損している場合はプロファイルをリカバリ
332
            logging.warning("Profile is corrupted, attempting recovery")
×
333
            if my_lib.chrome_util._recover_corrupted_profile(profile_path):
×
334
                logging.info("Profile recovery successful, will create new profile")
×
335
            else:
336
                logging.error("Profile recovery failed")
×
337

338
    if clean_profile:
×
339
        my_lib.chrome_util._cleanup_profile_lock(profile_path)
×
340

341
    # NOTE: 1回だけ自動リトライ
342
    try:
×
343
        return _create_driver_impl(
×
344
            profile_name, data_path, is_headless, use_subprocess, use_undetected, stealth_mode
345
        )
346
    except Exception as e:
×
347
        logging.warning("First attempt to create driver failed: %s", e)
×
348

349
        # コンテナ内で実行中の場合のみ、残った Chrome プロセスをクリーンアップ
350
        my_lib.chrome_util._cleanup_orphaned_chrome_processes_in_container()
×
351

352
        # プロファイルのロックファイルを削除
353
        my_lib.chrome_util._cleanup_profile_lock(profile_path)
×
354

355
        # 再度健全性チェック
356
        health = my_lib.chrome_util._check_profile_health(profile_path)
×
357
        if not health.is_healthy and auto_recover and (health.has_corrupted_json or health.has_corrupted_db):
×
358
            logging.warning("Profile still corrupted after first attempt, recovering")
×
359
            my_lib.chrome_util._recover_corrupted_profile(profile_path)
×
360

361
        return _create_driver_impl(
×
362
            profile_name, data_path, is_headless, use_subprocess, use_undetected, stealth_mode
363
        )
364

365

366
def xpath_exists(driver: WebDriver, xpath: str) -> bool:
1✔
367
    return len(driver.find_elements(selenium.webdriver.common.by.By.XPATH, xpath)) != 0
×
368

369

370
def get_text(
1✔
371
    driver: WebDriver,
372
    xpath: str,
373
    safe_text: str,
374
    wait: WebDriverWait[WebDriver] | None = None,
375
) -> str:
376
    if wait is not None:
×
377
        wait.until(
×
378
            selenium.webdriver.support.expected_conditions.presence_of_all_elements_located(
379
                (selenium.webdriver.common.by.By.XPATH, xpath)
380
            )
381
        )
382

383
    if len(driver.find_elements(selenium.webdriver.common.by.By.XPATH, xpath)) != 0:
×
384
        return driver.find_element(selenium.webdriver.common.by.By.XPATH, xpath).text.strip()
×
385
    else:
386
        return safe_text
×
387

388

389
def input_xpath(
1✔
390
    driver: WebDriver,
391
    xpath: str,
392
    text: str,
393
    wait: WebDriverWait[WebDriver] | None = None,
394
    is_warn: bool = True,
395
) -> bool:
396
    if wait is not None:
×
397
        wait.until(
×
398
            selenium.webdriver.support.expected_conditions.element_to_be_clickable(
399
                (selenium.webdriver.common.by.By.XPATH, xpath)
400
            )
401
        )
402
        time.sleep(0.05)
×
403

404
    if xpath_exists(driver, xpath):
×
405
        driver.find_element(selenium.webdriver.common.by.By.XPATH, xpath).send_keys(text)
×
406
        return True
×
407
    else:
408
        if is_warn:
×
409
            logging.warning("Element is not found: %s", xpath)
×
410
        return False
×
411

412

413
def click_xpath(
1✔
414
    driver: WebDriver,
415
    xpath: str,
416
    wait: WebDriverWait[WebDriver] | None = None,
417
    is_warn: bool = True,
418
    move: bool = False,
419
) -> bool:
420
    if wait is not None:
×
421
        wait.until(
×
422
            selenium.webdriver.support.expected_conditions.element_to_be_clickable(
423
                (selenium.webdriver.common.by.By.XPATH, xpath)
424
            )
425
        )
426
        time.sleep(0.05)
×
427

428
    if xpath_exists(driver, xpath):
×
429
        elem = driver.find_element(selenium.webdriver.common.by.By.XPATH, xpath)
×
430
        if move:
×
431
            action = selenium.webdriver.common.action_chains.ActionChains(driver)
×
432
            action.move_to_element(elem)
×
433
            action.perform()
×
434

435
        elem.click()
×
436
        return True
×
437
    else:
438
        if is_warn:
×
439
            logging.warning("Element is not found: %s", xpath)
×
440
        return False
×
441

442

443
def is_display(driver: WebDriver, xpath: str) -> bool:
1✔
444
    return (len(driver.find_elements(selenium.webdriver.common.by.By.XPATH, xpath)) != 0) and (
×
445
        driver.find_element(selenium.webdriver.common.by.By.XPATH, xpath).is_displayed()
446
    )
447

448

449
def random_sleep(sec: float) -> None:
1✔
450
    RATIO = 0.8
1✔
451

452
    time.sleep((sec * RATIO) + (sec * (1 - RATIO) * 2) * random.random())  # noqa: S311
1✔
453

454

455
def with_retry(
1✔
456
    func: Callable[[], T],
457
    max_retries: int = 3,
458
    delay: float = 1.0,
459
    exceptions: tuple[type[Exception], ...] = (Exception,),
460
    on_retry: Callable[[int, Exception], bool | None] | None = None,
461
) -> T:
462
    """リトライ付きで関数を実行
463

464
    全て失敗した場合は最後の例外を再スロー。
465
    呼び出し側で try/except してエラー処理を行う。
466

467
    Args:
468
        func: 実行する関数
469
        max_retries: 最大リトライ回数
470
        delay: リトライ間の待機秒数
471
        exceptions: リトライ対象の例外タプル
472
        on_retry: リトライ時のコールバック (attempt, exception)
473
            - None または True を返すとリトライを継続
474
            - False を返すとリトライを中止して例外を再スロー
475

476
    Returns:
477
        成功時は関数の戻り値
478

479
    Raises:
480
        最後の例外を再スロー
481

482
    """
483
    last_exception: Exception | None = None
1✔
484

485
    for attempt in range(max_retries):
1✔
486
        try:
1✔
487
            return func()
1✔
488
        except exceptions as e:
1✔
489
            last_exception = e
1✔
490
            if attempt < max_retries - 1:
1✔
491
                if on_retry:
1✔
492
                    should_continue = on_retry(attempt + 1, e)
1✔
493
                    if should_continue is False:
1✔
494
                        raise
1✔
495
                time.sleep(delay)
1✔
496

497
    if last_exception:
1✔
498
        raise last_exception
1✔
499
    raise RuntimeError("Unexpected state in with_retry")
×
500

501

502
def with_session_retry(
1✔
503
    func: Callable[[], T],
504
    driver_name: str,
505
    data_dir: pathlib.Path,
506
    *,
507
    max_retries: int = 1,
508
    clear_profile_on_error: bool = True,
509
    on_retry: Callable[[int, int], None] | None = None,
510
    before_retry: Callable[[], None] | None = None,
511
) -> T:
512
    """InvalidSessionIdException 発生時にプロファイル削除してリトライ
513

514
    ブラウザのセッションが無効になった場合(クラッシュ等)、
515
    プロファイルを削除して再起動することで復旧を試みる。
516

517
    Args:
518
        func: 実行する関数
519
        driver_name: Chrome プロファイル名
520
        data_dir: Selenium データディレクトリ
521
        max_retries: 最大リトライ回数 (デフォルト: 1)
522
        clear_profile_on_error: エラー時にプロファイルを削除するか
523
        on_retry: リトライ時のコールバック (attempt, max_retries) - ステータス更新用
524
        before_retry: リトライ前のコールバック - quit_selenium() 呼び出し用
525

526
    Returns:
527
        成功時は関数の戻り値
528

529
    Raises:
530
        InvalidSessionIdException: リトライ上限に達した場合
531

532
    """
533

534
    def retry_handler(attempt: int, exception: Exception) -> bool | None:
1✔
535
        if before_retry:
1✔
536
            before_retry()
1✔
537

538
        if attempt <= max_retries and clear_profile_on_error:
1✔
539
            logging.warning(
1✔
540
                "セッションエラーが発生しました。プロファイルを削除してリトライします(%d/%d)",
541
                attempt,
542
                max_retries,
543
            )
544
            if on_retry:
1✔
545
                on_retry(attempt, max_retries)
1✔
546
            my_lib.chrome_util.delete_profile(driver_name, data_dir)
1✔
547
            return True
1✔
548
        return False
1✔
549

550
    return with_retry(
1✔
551
        func,
552
        max_retries=max_retries + 1,
553
        delay=0,
554
        exceptions=(selenium.common.exceptions.InvalidSessionIdException,),
555
        on_retry=retry_handler,
556
    )
557

558

559
def wait_patiently(
1✔
560
    driver: WebDriver,
561
    wait: WebDriverWait[WebDriver],
562
    target: Any,
563
) -> None:
564
    error: selenium.common.exceptions.TimeoutException | None = None
×
565
    for i in range(WAIT_RETRY_COUNT + 1):
×
566
        try:
×
567
            wait.until(target)
×
568
            return
×
569
        except selenium.common.exceptions.TimeoutException as e:
×
570
            logging.warning(
×
571
                "タイムアウトが発生しました。(%s in %s line %d)",
572
                inspect.stack()[1].function,
573
                inspect.stack()[1].filename,
574
                inspect.stack()[1].lineno,
575
            )
576
            error = e
×
577

578
            logging.info(i)
×
579
            if i != WAIT_RETRY_COUNT:
×
580
                logging.info("refresh")
×
581
                driver.refresh()
×
582

583
    if error is not None:
×
584
        raise error
×
585

586

587
def dump_page(
1✔
588
    driver: WebDriver,
589
    index: int,
590
    dump_path: pathlib.Path,
591
    stack_index: int = 1,
592
) -> None:
593
    name = inspect.stack()[stack_index].function.replace("<", "").replace(">", "")
×
594

595
    dump_path.mkdir(parents=True, exist_ok=True)
×
596

597
    png_path = dump_path / f"{name}_{index:02d}.png"
×
598
    htm_path = dump_path / f"{name}_{index:02d}.htm"
×
599

600
    driver.save_screenshot(str(png_path))
×
601

602
    with htm_path.open("w", encoding="utf-8") as f:
×
603
        f.write(driver.page_source)
×
604

605
    logging.info(
×
606
        "page dump: %02d from %s in %s line %d",
607
        index,
608
        inspect.stack()[stack_index].function,
609
        inspect.stack()[stack_index].filename,
610
        inspect.stack()[stack_index].lineno,
611
    )
612

613

614
def clear_cache(driver: WebDriver) -> None:
1✔
615
    driver.execute_cdp_cmd("Network.clearBrowserCache", {})
×
616

617

618
def set_japanese_locale(driver: WebDriver) -> None:
1✔
619
    """CDP を使って日本語ロケールを強制設定
620

621
    Chrome の起動オプションだけでは言語設定が変わってしまうことがあるため、
622
    CDP を使って Accept-Language ヘッダーを強制的に日本語に設定する。
623

624
    NOTE: Emulation.setLocaleOverride は使用しない。Chrome 142 + 日本語ロケールの
625
    組み合わせで、内蔵拡張機能(Web Store 等)の manifest.name が Preferences 保存時に
626
    毎回 UTF-8 多重エンコードされ、Preferences ファイルが際限なく肥大化するバグがあるため。
627
    --lang=ja-JP オプションと Accept-Language ヘッダーで十分に日本語環境を維持できる。
628
    """
629
    try:
×
630
        # NOTE: Network.setExtraHTTPHeaders は Network.enable を先に呼ばないと機能しない
631
        driver.execute_cdp_cmd("Network.enable", {})
×
632
        driver.execute_cdp_cmd(
×
633
            "Network.setExtraHTTPHeaders",
634
            {"headers": {"Accept-Language": "ja-JP,ja;q=0.9"}},
635
        )
636
        logging.debug("Japanese locale set via CDP")
×
637
    except Exception:
×
638
        logging.warning("Failed to set Japanese locale via CDP")
×
639

640

641
def _get_stealth_user_agent(driver: WebDriver) -> str:
1✔
642
    """ブラウザの User-Agent を取得し、ボット検出回避用に修正.
643

644
    - OS 部分を Windows に変更
645
    - HeadlessChrome を Chrome に変更
646

647
    Args:
648
        driver: WebDriver インスタンス
649

650
    Returns:
651
        修正された User-Agent 文字列
652

653
    """
654
    original_ua = driver.execute_script("return navigator.userAgent")
×
655
    logging.debug("Original User-Agent: %s", original_ua)
×
656

657
    # OS 部分を Windows に変更
658
    pattern = r"\([^)]*(?:Linux|Macintosh|X11)[^)]*\)"
×
659
    replacement = "(Windows NT 10.0; Win64; x64)"
×
660
    modified_ua = re.sub(pattern, replacement, original_ua)
×
661

662
    # HeadlessChrome を Chrome に変更
663
    modified_ua = modified_ua.replace("HeadlessChrome", "Chrome")
×
664

665
    logging.debug("Modified User-Agent: %s", modified_ua)
×
666
    return modified_ua
×
667

668

669
def set_stealth_mode(driver: WebDriver) -> None:
1✔
670
    """ボット検出回避のための CDP 設定を適用.
671

672
    ヨドバシ等のボット検出は User-Agent をチェックしているため、
673
    OS を Windows に偽装し、HeadlessChrome を Chrome に変更することで回避できる。
674

675
    Args:
676
        driver: WebDriver インスタンス
677

678
    """
679
    try:
×
680
        modified_ua = _get_stealth_user_agent(driver)
×
681
        driver.execute_cdp_cmd(
×
682
            "Network.setUserAgentOverride",
683
            {
684
                "userAgent": modified_ua,
685
                "acceptLanguage": "ja-JP,ja;q=0.9,en-US;q=0.8,en;q=0.7",
686
                "platform": "Win32",
687
            },
688
        )
689
        logging.debug("Stealth mode enabled (User-Agent override applied)")
×
690
    except Exception:
×
691
        logging.warning("Failed to enable stealth mode")
×
692

693

694
def clean_dump(dump_path: pathlib.Path, keep_days: int = 1) -> None:
1✔
695
    if not dump_path.exists():
1✔
696
        return
1✔
697

698
    time_threshold = datetime.timedelta(keep_days)
1✔
699

700
    for item in dump_path.iterdir():
1✔
701
        if not item.is_file():
1✔
702
            continue
1✔
703
        try:
1✔
704
            time_diff = datetime.datetime.now(datetime.UTC) - datetime.datetime.fromtimestamp(
1✔
705
                item.stat().st_mtime, datetime.UTC
706
            )
707
        except FileNotFoundError:
×
708
            # ファイルが別プロセスにより削除された場合(SQLiteの一時ファイルなど)
709
            continue
×
710
        if time_diff > time_threshold:
1✔
711
            logging.warning("remove %s [%s day(s) old].", item.absolute(), f"{time_diff.days:,}")
1✔
712

713
            item.unlink(missing_ok=True)
1✔
714

715

716
def get_memory_info(driver: WebDriver) -> dict[str, Any]:
1✔
717
    """ブラウザのメモリ使用量を取得(単位: KB)"""
718
    total_bytes = subprocess.Popen(  # noqa: S602
×
719
        "smem -t -c pss -P chrome | tail -n 1",  # noqa: S607
720
        shell=True,
721
        stdout=subprocess.PIPE,
722
    ).communicate()[0]
723
    total = int(str(total_bytes, "utf-8").strip())  # smem の出力は KB 単位
×
724

725
    try:
×
726
        memory_info = driver.execute_cdp_cmd("Memory.getAllTimeSamplingProfile", {})
×
727
        heap_usage = driver.execute_cdp_cmd("Runtime.getHeapUsage", {})
×
728

729
        heap_used = heap_usage.get("usedSize", 0) // 1024  # bytes → KB
×
730
        heap_total = heap_usage.get("totalSize", 0) // 1024  # bytes → KB
×
731
    except Exception as e:
×
732
        logging.debug("Failed to get memory usage: %s", e)
×
733

734
        memory_info = None
×
735
        heap_used = 0
×
736
        heap_total = 0
×
737

738
    return {
×
739
        "total": total,
740
        "heap_used": heap_used,
741
        "heap_total": heap_total,
742
        "memory_info": memory_info,
743
    }
744

745

746
def log_memory_usage(driver: WebDriver) -> None:
1✔
747
    mem_info = get_memory_info(driver)
×
748
    logging.info(
×
749
        "Chrome memory: %s MB (JS heap: %s MB)",
750
        f"""{mem_info["total"] // 1024:,}""",
751
        f"""{mem_info["heap_used"] // 1024:,}""",
752
    )
753

754

755
def _warmup(
1✔
756
    driver: WebDriver,
757
    keyword: str,
758
    url_pattern: str,
759
    sleep_sec: int = 3,
760
) -> None:
761
    # NOTE: ダミーアクセスを行って BOT ではないと思わせる。(効果なさそう...)
762
    driver.get("https://www.yahoo.co.jp/")
×
763
    time.sleep(sleep_sec)
×
764

765
    driver.find_element(selenium.webdriver.common.by.By.XPATH, '//input[@name="p"]').send_keys(keyword)
×
766
    driver.find_element(selenium.webdriver.common.by.By.XPATH, '//input[@name="p"]').send_keys(
×
767
        selenium.webdriver.common.keys.Keys.ENTER
768
    )
769

770
    time.sleep(sleep_sec)
×
771

772
    driver.find_element(
×
773
        selenium.webdriver.common.by.By.XPATH, f'//a[contains(@href, "{url_pattern}")]'
774
    ).click()
775

776
    time.sleep(sleep_sec)
×
777

778

779
class browser_tab:
1✔
780
    """新しいブラウザタブで URL を開くコンテキストマネージャ"""
781

782
    def __init__(self, driver: WebDriver, url: str) -> None:
1✔
783
        """初期化
784

785
        Args:
786
            driver: WebDriver インスタンス
787
            url: 開く URL
788

789
        """
790
        self.driver = driver
1✔
791
        self.url = url
1✔
792
        self.original_window: str | None = None
1✔
793

794
    def __enter__(self) -> None:
1✔
795
        """新しいタブを開いて URL にアクセス"""
796
        self.original_window = self.driver.current_window_handle
1✔
797
        self.driver.execute_script("window.open('');")
1✔
798
        self.driver.switch_to.window(self.driver.window_handles[-1])
1✔
799
        try:
1✔
800
            self.driver.get(self.url)
1✔
801
        except Exception:
×
802
            # NOTE: URL読み込みに失敗した場合もクリーンアップしてから例外を再送出
803
            self._cleanup()
×
804
            raise
×
805

806
    def _cleanup(self) -> None:
1✔
807
        """タブを閉じて元のウィンドウに戻る"""
808
        try:
1✔
809
            # 余分なタブを閉じる
810
            while len(self.driver.window_handles) > 1:
1✔
811
                self.driver.switch_to.window(self.driver.window_handles[-1])
1✔
812
                self.driver.close()
1✔
813
            if self.original_window is not None:
1✔
814
                self.driver.switch_to.window(self.original_window)
1✔
815
            time.sleep(0.1)
1✔
816
        except Exception:
1✔
817
            # NOTE: Chromeがクラッシュした場合は無視(既に終了しているため操作不可)
818
            logging.exception("タブのクリーンアップに失敗しました(Chromeがクラッシュした可能性があります)")
1✔
819

820
    def _recover_from_error(self) -> None:
1✔
821
        """エラー後にブラウザの状態を回復する"""
822
        try:
×
823
            # ページロードタイムアウトをリセット(負の値になっている可能性があるため)
824
            self.driver.set_page_load_timeout(30)
×
825

826
            # about:blank に移動してレンダラーの状態をリセット
827
            self.driver.get("about:blank")
×
828
            time.sleep(0.5)
×
829
        except Exception:
×
830
            logging.warning("ブラウザの回復に失敗しました")
×
831

832
    def __exit__(
1✔
833
        self,
834
        exception_type: type[BaseException] | None,
835
        exception_value: BaseException | None,
836
        traceback: types.TracebackType | None,
837
    ) -> None:
838
        """タブを閉じて元のウィンドウに戻る"""
839
        self._cleanup()
1✔
840

841
        # 例外が発生した場合はブラウザの状態を回復
842
        if exception_type is not None:
1✔
843
            self._recover_from_error()
×
844

845

846
class error_handler:
1✔
847
    """Selenium操作時のエラーハンドリング用コンテキストマネージャ
848

849
    エラー発生時に自動でログ出力、スクリーンショット取得、コールバック呼び出しを行う。
850

851
    Args:
852
        driver: WebDriver インスタンス
853
        message: ログに出力するエラーメッセージ
854
        on_error: エラー時に呼ばれるコールバック関数
855
            (exception, screenshot: PIL.Image.Image | None, page_source: str | None) -> None
856
        capture_screenshot: スクリーンショットを自動取得するか(デフォルト: True)
857
        reraise: 例外を再送出するか(デフォルト: True)
858

859
    Attributes:
860
        exception: 発生した例外(エラーがなければ None)
861
        screenshot: 取得したスクリーンショット(PIL.Image.Image、取得失敗時は None)
862
        page_source: 取得したページソース(取得失敗時は None)
863

864
    Examples:
865
        基本的な使用方法::
866

867
            with my_lib.selenium_util.error_handler(driver, message="ログイン処理に失敗") as handler:
868
                driver.get(login_url)
869
                driver.find_element(...).click()
870

871
        コールバック付き(Slack通知など)::
872

873
            def notify(exc, screenshot, page_source):
874
                slack.notify_error_with_page(config, "エラー発生", exc, screenshot, page_source)
875

876
            with my_lib.selenium_util.error_handler(
877
                driver,
878
                message="クロール処理に失敗",
879
                on_error=notify,
880
            ):
881
                crawl_page(driver)
882

883
        例外を抑制して続行::
884

885
            with my_lib.selenium_util.error_handler(driver, reraise=False) as handler:
886
                risky_operation()
887

888
            if handler.exception:
889
                logging.warning("処理をスキップしました")
890

891
    """
892

893
    def __init__(
1✔
894
        self,
895
        driver: WebDriver,
896
        message: str = "Selenium operation failed",
897
        on_error: Callable[[Exception, PIL.Image.Image | None, str | None], None] | None = None,
898
        capture_screenshot: bool = True,
899
        reraise: bool = True,
900
    ) -> None:
901
        """初期化"""
902
        self.driver = driver
1✔
903
        self.message = message
1✔
904
        self.on_error = on_error
1✔
905
        self.capture_screenshot = capture_screenshot
1✔
906
        self.reraise = reraise
1✔
907
        self.exception: Exception | None = None
1✔
908
        self.screenshot: PIL.Image.Image | None = None
1✔
909
        self.page_source: str | None = None
1✔
910

911
    def __enter__(self) -> Self:
1✔
912
        """コンテキストマネージャの開始"""
913
        return self
1✔
914

915
    def __exit__(
1✔
916
        self,
917
        exception_type: type[BaseException] | None,
918
        exception_value: BaseException | None,
919
        traceback: types.TracebackType | None,
920
    ) -> bool:
921
        """コンテキストマネージャの終了、エラー処理を実行"""
922
        if exception_value is None:
1✔
923
            return False
1✔
924

925
        # 例外を記録
926
        if isinstance(exception_value, Exception):
1✔
927
            self.exception = exception_value
1✔
928
        else:
929
            # BaseException(KeyboardInterrupt など)は処理せず再送出
930
            return False
×
931

932
        # ログ出力
933
        logging.exception(self.message)
1✔
934

935
        # スクリーンショット・ページソース取得
936
        if self.capture_screenshot:
1✔
937
            try:
1✔
938
                self.page_source = self.driver.page_source
1✔
939
            except Exception:
×
940
                logging.debug("Failed to capture page source for error handling")
×
941
            try:
1✔
942
                screenshot_bytes = self.driver.get_screenshot_as_png()
1✔
943
                self.screenshot = PIL.Image.open(io.BytesIO(screenshot_bytes))
1✔
944
            except Exception:
1✔
945
                logging.debug("Failed to capture screenshot for error handling")
1✔
946

947
        # コールバック呼び出し
948
        if self.on_error is not None:
1✔
949
            try:
1✔
950
                self.on_error(self.exception, self.screenshot, self.page_source)
1✔
951
            except Exception:
×
952
                logging.exception("Error in on_error callback")
×
953

954
        # reraise=False なら例外を抑制
955
        return not self.reraise
1✔
956

957

958
def _is_chrome_related_process(process: psutil.Process) -> bool:
1✔
959
    """プロセスがChrome関連かどうかを判定"""
960
    try:
1✔
961
        process_name = process.name().lower()
1✔
962
        # Chrome関連のプロセス名パターン
963
        chrome_patterns = ["chrome", "chromium", "google-chrome", "undetected_chro"]
1✔
964
        # chromedriverは除外
965
        if "chromedriver" in process_name:
1✔
966
            return False
1✔
967
        return any(pattern in process_name for pattern in chrome_patterns)
1✔
968
    except (psutil.NoSuchProcess, psutil.AccessDenied):
1✔
969
        return False
1✔
970

971

972
def _get_chrome_processes_by_pgid(chromedriver_pid: int, existing_pids: set[int]) -> list[int]:
1✔
973
    """プロセスグループIDで追加のChrome関連プロセスを取得"""
974
    additional_pids = []
×
975
    try:
×
976
        pgid = os.getpgid(chromedriver_pid)
×
977
        for proc in psutil.process_iter(["pid", "name", "ppid"]):
×
978
            if proc.info["pid"] in existing_pids:
×
979
                continue
×
980
            try:
×
981
                if os.getpgid(proc.info["pid"]) == pgid:
×
982
                    proc_obj = psutil.Process(proc.info["pid"])
×
983
                    if _is_chrome_related_process(proc_obj):
×
984
                        additional_pids.append(proc.info["pid"])
×
985
                        logging.debug(
×
986
                            "Found Chrome-related process by pgid: PID %d, name: %s",
987
                            proc.info["pid"],
988
                            proc.info["name"],
989
                        )
990
            except (psutil.NoSuchProcess, psutil.AccessDenied, OSError):
×
991
                pass
×
992
    except (OSError, psutil.NoSuchProcess):
×
993
        logging.debug("Failed to get process group ID for chromedriver")
×
994
    return additional_pids
×
995

996

997
def _get_chrome_related_processes(driver: WebDriver) -> list[int]:
1✔
998
    """Chrome関連の全子プロセスを取得
999

1000
    undetected_chromedriver 使用時、Chrome プロセスは chromedriver の子ではなく
1001
    Python プロセスの直接の子として起動されることがあるため、両方を検索する。
1002
    """
1003
    chrome_pids = set()
×
1004

1005
    # 1. driver.service.process の子プロセスを検索
1006
    # NOTE: Chrome/Firefox WebDriver には service 属性があるが、型スタブでは定義されていない
1007
    try:
×
1008
        if hasattr(driver, "service") and driver.service and hasattr(driver.service, "process"):
×
1009
            process = driver.service.process
×
1010
            if process and hasattr(process, "pid"):
×
1011
                chromedriver_pid = process.pid
×
1012

1013
                # psutilでプロセス階層を取得
1014
                parent_process = psutil.Process(chromedriver_pid)
×
1015
                children = parent_process.children(recursive=True)
×
1016

1017
                for child in children:
×
1018
                    chrome_pids.add(child.pid)
×
1019
                    logging.debug(
×
1020
                        "Found Chrome-related process (service child): PID %d, name: %s",
1021
                        child.pid,
1022
                        child.name(),
1023
                    )
1024
    except Exception:
×
1025
        logging.exception("Failed to get Chrome-related processes from service")
×
1026

1027
    # 2. 現在の Python プロセスの全子孫から Chrome 関連プロセスを検索
1028
    try:
×
1029
        current_process = psutil.Process()
×
1030
        all_children = current_process.children(recursive=True)
×
1031

1032
        for child in all_children:
×
1033
            if child.pid in chrome_pids:
×
1034
                continue
×
1035
            try:
×
1036
                if _is_chrome_related_process(child):
×
1037
                    chrome_pids.add(child.pid)
×
1038
                    logging.debug(
×
1039
                        "Found Chrome-related process (python child): PID %d, name: %s",
1040
                        child.pid,
1041
                        child.name(),
1042
                    )
1043
            except (psutil.NoSuchProcess, psutil.AccessDenied):
×
1044
                pass
×
1045
    except Exception:
×
1046
        logging.exception("Failed to get Chrome-related processes from python children")
×
1047

1048
    return list(chrome_pids)
×
1049

1050

1051
def _send_signal_to_processes(pids: list[int], sig: signal.Signals, signal_name: str) -> None:
1✔
1052
    """プロセスリストに指定されたシグナルを送信"""
1053
    errors = []
×
1054
    for pid in pids:
×
1055
        try:
×
1056
            # プロセス名を取得
1057
            try:
×
1058
                process = psutil.Process(pid)
×
1059
                process_name = process.name()
×
1060
            except (psutil.NoSuchProcess, psutil.AccessDenied):
×
1061
                process_name = "unknown"
×
1062

1063
            if sig == signal.SIGKILL:
×
1064
                # プロセスがまだ存在するかチェック
1065
                os.kill(pid, 0)  # シグナル0は存在確認
×
1066
            os.kill(pid, sig)
×
1067
            logging.info("Sent %s to process: PID %d (%s)", signal_name, pid, process_name)
×
1068
        except (ProcessLookupError, OSError) as e:
×
1069
            # プロセスが既に終了している場合は無視
1070
            errors.append((pid, e))
×
1071

1072
    # エラーが発生した場合はまとめてログ出力
1073
    if errors:
×
1074
        logging.debug("Failed to send %s to some processes: %s", signal_name, errors)
×
1075

1076

1077
def _terminate_chrome_processes(chrome_pids: list[int], timeout: float = 5.0) -> None:
1✔
1078
    """Chrome関連プロセスを段階的に終了
1079

1080
    Args:
1081
        chrome_pids: 終了対象のプロセスIDリスト
1082
        timeout: SIGTERM後にプロセス終了を待機する最大時間(秒)
1083

1084
    """
1085
    if not chrome_pids:
×
1086
        return
×
1087

1088
    # 優雅な終了(SIGTERM)
1089
    _send_signal_to_processes(chrome_pids, signal.SIGTERM, "SIGTERM")
×
1090

1091
    # プロセスの終了を待機(ポーリング)
1092
    remaining_pids = list(chrome_pids)
×
1093
    poll_interval = 0.2
×
1094
    elapsed = 0.0
×
1095

1096
    while remaining_pids and elapsed < timeout:
×
1097
        time.sleep(poll_interval)
×
1098
        elapsed += poll_interval
×
1099

1100
        # まだ生存しているプロセスをチェック
1101
        still_alive = []
×
1102
        for pid in remaining_pids:
×
1103
            try:
×
1104
                if psutil.pid_exists(pid):
×
1105
                    process = psutil.Process(pid)
×
1106
                    if process.is_running() and process.status() != psutil.STATUS_ZOMBIE:
×
1107
                        still_alive.append(pid)
×
1108
            except (psutil.NoSuchProcess, psutil.AccessDenied):
×
1109
                pass
×
1110

1111
        remaining_pids = still_alive
×
1112

1113
    # タイムアウト後もまだ残っているプロセスにのみ SIGKILL を送信
1114
    if remaining_pids:
×
1115
        logging.warning(
×
1116
            "Chrome processes still alive after %.1fs, sending SIGKILL to %d processes",
1117
            elapsed,
1118
            len(remaining_pids),
1119
        )
1120
        _send_signal_to_processes(remaining_pids, signal.SIGKILL, "SIGKILL")
×
1121

1122

1123
def _reap_single_process(pid: int) -> None:
1✔
1124
    """単一プロセスをwaitpidで回収"""
1125
    try:
×
1126
        # ノンブロッキングでwaitpid
1127
        result_pid, status = os.waitpid(pid, os.WNOHANG)
×
1128
        if result_pid == pid:
×
1129
            # プロセス名を取得
1130
            try:
×
1131
                process = psutil.Process(pid)
×
1132
                process_name = process.name()
×
1133
            except (psutil.NoSuchProcess, psutil.AccessDenied):
×
1134
                process_name = "unknown"
×
1135
            logging.debug("Reaped Chrome process: PID %d (%s)", pid, process_name)
×
1136
    except (ChildProcessError, OSError):
×
1137
        # 子プロセスでない場合や既に回収済みの場合は無視
1138
        pass
×
1139

1140

1141
def _reap_chrome_processes(chrome_pids: list[int]) -> None:
1✔
1142
    """Chrome関連プロセスを明示的に回収してゾンビ化を防ぐ"""
1143
    for pid in chrome_pids:
×
1144
        _reap_single_process(pid)
×
1145

1146

1147
def _get_remaining_chrome_pids(chrome_pids: list[int]) -> list[int]:
1✔
1148
    """指定されたPIDリストから、まだ生存しているChrome関連プロセスを取得"""
1149
    remaining = []
×
1150
    for pid in chrome_pids:
×
1151
        try:
×
1152
            if psutil.pid_exists(pid):
×
1153
                process = psutil.Process(pid)
×
1154
                if (
×
1155
                    process.is_running()
1156
                    and process.status() != psutil.STATUS_ZOMBIE
1157
                    and _is_chrome_related_process(process)
1158
                ):
1159
                    remaining.append(pid)
×
1160
        except (psutil.NoSuchProcess, psutil.AccessDenied):
×
1161
            pass
×
1162
    return remaining
×
1163

1164

1165
def _wait_for_processes_with_check(
1✔
1166
    chrome_pids: list[int],
1167
    timeout: float,
1168
    poll_interval: float = 0.2,
1169
    log_interval: float = 1.0,
1170
) -> list[int]:
1171
    """プロセスの終了を待機しつつ、残存プロセスをチェック
1172

1173
    Args:
1174
        chrome_pids: 監視対象のプロセスIDリスト
1175
        timeout: 最大待機時間(秒)
1176
        poll_interval: チェック間隔(秒)
1177
        log_interval: ログ出力間隔(秒)
1178

1179
    Returns:
1180
        タイムアウト後も残存しているプロセスIDのリスト
1181

1182
    """
1183
    elapsed = 0.0
×
1184
    last_log_time = 0.0
×
1185
    remaining_pids = list(chrome_pids)
×
1186

1187
    while remaining_pids and elapsed < timeout:
×
1188
        time.sleep(poll_interval)
×
1189
        elapsed += poll_interval
×
1190
        remaining_pids = _get_remaining_chrome_pids(remaining_pids)
×
1191

1192
        if remaining_pids and (elapsed - last_log_time) >= log_interval:
×
1193
            logging.info(
×
1194
                "Found %d remaining Chrome processes after %.0fs",
1195
                len(remaining_pids),
1196
                elapsed,
1197
            )
1198
            last_log_time = elapsed
×
1199

1200
    return remaining_pids
×
1201

1202

1203
def quit_driver_gracefully(
1✔
1204
    driver: WebDriver | None,
1205
    wait_sec: float = 5.0,
1206
    sigterm_wait_sec: float = 5.0,
1207
    sigkill_wait_sec: float = 5.0,
1208
) -> None:
1209
    """Chrome WebDriverを確実に終了する
1210

1211
    終了フロー:
1212
    1. driver.quit() を呼び出し
1213
    2. wait_sec 秒待機しつつプロセス終了をチェック
1214
    3. 残存プロセスがあれば SIGTERM を送信
1215
    4. sigterm_wait_sec 秒待機しつつプロセス終了をチェック
1216
    5. 残存プロセスがあれば SIGKILL を送信
1217
    6. sigkill_wait_sec 秒待機
1218

1219
    Args:
1220
        driver: 終了する WebDriver インスタンス
1221
        wait_sec: quit 後にプロセス終了を待機する秒数(デフォルト: 5秒)
1222
        sigterm_wait_sec: SIGTERM 後にプロセス終了を待機する秒数(デフォルト: 5秒)
1223
        sigkill_wait_sec: SIGKILL 後にプロセス回収を待機する秒数(デフォルト: 5秒)
1224

1225
    """
1226
    if driver is None:
1✔
1227
        return
1✔
1228

1229
    # quit前にChrome関連プロセスを記録
1230
    chrome_pids_before = _get_chrome_related_processes(driver)
1✔
1231

1232
    try:
1✔
1233
        # WebDriverの正常終了を試行(これがタブのクローズも含む)
1234
        driver.quit()
1✔
1235
        logging.info("WebDriver quit successfully")
1✔
1236
    except Exception:
1✔
1237
        logging.warning("Failed to quit driver normally", exc_info=True)
1✔
1238
    finally:
1239
        # undetected_chromedriver の __del__ がシャットダウン時に再度呼ばれるのを防ぐ
1240
        if hasattr(driver, "_has_quit"):
1✔
1241
            driver._has_quit = True  # type: ignore[attr-defined]
1✔
1242

1243
    # ChromeDriverサービスの停止を試行
1244
    try:
1✔
1245
        if hasattr(driver, "service") and driver.service and hasattr(driver.service, "stop"):
1✔
1246
            driver.service.stop()  # type: ignore[call-non-callable]
×
1247
    except (ConnectionResetError, OSError):
×
1248
        # Chrome が既に終了している場合は無視
1249
        logging.debug("Chrome service already stopped")
×
1250
    except Exception:
×
1251
        logging.warning("Failed to stop Chrome service", exc_info=True)
×
1252

1253
    # Step 1: quit 後に wait_sec 秒待機しつつプロセス終了をチェック
1254
    remaining_pids = _wait_for_processes_with_check(chrome_pids_before, wait_sec)
1✔
1255

1256
    if not remaining_pids:
1✔
1257
        logging.debug("All Chrome processes exited normally")
1✔
1258
        return
1✔
1259

1260
    # Step 2: 残存プロセスに SIGTERM を送信
1261
    logging.info(
×
1262
        "Found %d remaining Chrome processes after %.0fs, sending SIGTERM",
1263
        len(remaining_pids),
1264
        wait_sec,
1265
    )
1266
    _send_signal_to_processes(remaining_pids, signal.SIGTERM, "SIGTERM")
×
1267

1268
    # Step 3: SIGTERM 後に sigterm_wait_sec 秒待機しつつプロセス終了をチェック
1269
    remaining_pids = _wait_for_processes_with_check(remaining_pids, sigterm_wait_sec)
×
1270

1271
    if not remaining_pids:
×
1272
        logging.info("All Chrome processes exited after SIGTERM")
×
1273
        _reap_chrome_processes(chrome_pids_before)
×
1274
        return
×
1275

1276
    # Step 4: 残存プロセスに SIGKILL を送信
1277
    logging.warning(
×
1278
        "Chrome processes still alive after SIGTERM + %.1fs, sending SIGKILL to %d processes",
1279
        sigterm_wait_sec,
1280
        len(remaining_pids),
1281
    )
1282
    _send_signal_to_processes(remaining_pids, signal.SIGKILL, "SIGKILL")
×
1283

1284
    # Step 5: SIGKILL 後に sigkill_wait_sec 秒待機してプロセス回収
1285
    time.sleep(sigkill_wait_sec)
×
1286
    _reap_chrome_processes(chrome_pids_before)
×
1287

1288
    # 最終チェック:まだ残っているプロセスがあるか確認
1289
    still_remaining = _get_remaining_chrome_pids(remaining_pids)
×
1290

1291
    # 回収できなかったプロセスについて警告
1292
    if still_remaining:
×
1293
        for pid in still_remaining:
×
1294
            try:
×
1295
                process = psutil.Process(pid)
×
1296
                logging.warning("Failed to collect Chrome-related process: PID %d (%s)", pid, process.name())
×
1297
            except (psutil.NoSuchProcess, psutil.AccessDenied):
×
1298
                pass
×
1299

1300

1301
if __name__ == "__main__":
1302
    import pathlib
1303

1304
    import docopt
1305
    import selenium.webdriver.support.wait
1306

1307
    import my_lib.config
1308
    import my_lib.logger
1309

1310
    assert __doc__ is not None  # noqa: S101
1311
    args = docopt.docopt(__doc__)
1312

1313
    config_file = args["-c"]
1314
    debug_mode = args["-D"]
1315

1316
    my_lib.logger.init("test", level=logging.DEBUG if debug_mode else logging.INFO)
1317

1318
    config = my_lib.config.load(config_file)
1319

1320
    driver = create_driver("test", pathlib.Path(config["data"]["selenium"]))
1321
    wait = selenium.webdriver.support.wait.WebDriverWait(driver, 5)
1322

1323
    driver.get("https://www.google.com/")
1324
    wait.until(
1325
        selenium.webdriver.support.expected_conditions.presence_of_element_located(
1326
            (selenium.webdriver.common.by.By.XPATH, '//input[contains(@value, "Google")]')
1327
        )
1328
    )
1329

1330
    quit_driver_gracefully(driver)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc