Skip to content

実戦ケース集

この章では、実際のプロジェクトにそのまま流用できる 4 つのビジネススクリプトテンプレートを紹介します。


ケース 1:アカウント情報とパスワードによる自動ログイン

シナリオ: 各種プラットフォーム(EC 管理画面、SNS、広告アカウントなど)への自動ログイン。

python
# (Python コードの構造を維持しつつ、コメントを日本語化)
def auto_login():
    driver = get_driver_from_mbbrowser(SESSION_ID)
    wait = WebDriverWait(driver, 20)

    try:
        # 1. ログインページを開く
        driver.get("https://example.com/login")

        # 2. 入力欄が表示されるまで待ち、アカウントを入力
        username_input = wait.until(EC.visibility_of_element_located((By.ID, "username")))
        username_input.send_keys("user@email.com")

        # 3. パスワードを入力
        driver.find_element(By.ID, "password").send_keys("password123")

        # 4. ログインボタンをクリック
        driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()

        # 5. ログイン成功の兆候を待機
        wait.until(EC.url_contains("/dashboard"))
        print("✅ ログイン成功!")
    finally:
        driver.quit()

シナリオ: 取得済みの Cookie を注入し、ログインフォームを無視して直接内部ページへアクセスします。

python
# Cookie の注入
for cookie in cookies:
    driver.add_cookie(cookie)
driver.refresh()

ケース 3:マルチアカウントの並列実行

Python の threading モジュールを使用して、複数の Mbbrowser 環境を同時に制御します。

python
import threading
# (並列処理ロジックの説明とコード)

IMPORTANT

並列数の推奨目安: メモリ 16GB の場合、10〜15 環境程度を上限にすることをお勧めします。過度な並列はレスポンス低下の原因となります。


ケース 4:生産レベルのスライプトフレームワーク

エラー処理やリトライメカニズムを備えた、より堅牢な実装方法を紹介します。


TIP

🎉 おめでとうございます!これで Mbbrowser + Selenium の自動化技術スタックを完全にマスターしました。