Back to list
dev_to 2026年4月25日

Selenium with Python: 現代のテスト自動化における実用的なチートシート

🧪 Selenium with Python: A Practical Cheat Sheet for Modern Test Automation

Translated: 2026/4/25 0:00:54
seleniumpythontest-automationpytestqa-engineering

Japanese Translation

クオリティアセインエンジニアとしての自動化への参入、あるいは SDET としてのリフレッシュが欲しい方へ。このチートシートは、実際の業務で本当に使用する Selenium と Python の必須事項を網羅しています。 理論の山登りはありません。古いパターンもありません。コピーして、アダプトして、スケーリングできる実践的な例だけです。 ご阅读をありがとう!新しい投稿を受け取り、私の仕事をサポートするために無料で購読してください。 ✅ セットアップとインストール Selenium のインストール: pip install selenium 正しいブラウザドライバーがシステムの PATH にあることを確認してください: Chrome → chromedriver Firefox → geckodriver Edge → msedgedriver 🚀 ブラウザの起動 Chrome の起動 driver = webdriver.Chrome() driver.get("https://example.com") ヘッドレスモード(CI に推奨) from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("--headless") options.add_argument("--window-size=1920,1080") driver = webdriver.Chrome(options=options) 🔍 要素の選択(最も重要なスキル) 常に信頼性が高く、安定したロケータを優先してください。 from selenium.webdriver.common.by import By 推奨されるロケータ driver.find_element(By.NAME, "email") driver.find_element(By.CSS_SELECTOR, ".login-button") driver.find_element(By.CSS_SELECTOR, "input[type='password']") XPath(限定的に使用してください) driver.find_element(By.XPATH, "//button[text()='Login']") ✅ ヒント:テストが頻繁に壊れる場合、それはおそらくロケータの問題です。 ✍️ ユーザー操作 element.click() user@test.com") ⏳ 要素の待ち時間(不可避) 明示的なウェイト(ベストプラクティス) 一般的な条件: EC.presence_of_element_located ❌ time.sleep() を避けてください — これは不安定なテストの原因になります。 📄 フォームとドロップダウン from selenium.webdriver.support.ui import Select 🔔 警報、フレーム、ウィンドウ 警報 alert = driver.switch_to.alert iFrames driver.switch_to.frame("frameName") 複数のタブ 📜 スクロールと JavaScript driver.execute_script("window.scrollTo(0, document.body.scrollHeight)") 📷 スクリーンショット(失敗したテスト用) driver.save_screenshot("failure.png") 🧹 クリーニング 🚀 Selenium + Pytest: 本番的な自動化セットアップ pip install pytest 🧱 プロジェクト構造(推奨) 🔁 Pytest WebDriver フェイクセット conftest.py @pytest.fixture ✅ 自動的にセットアップとテダウンを管理します 🧪 テストの記述 def test_valid_login(driver): https://example.com/login") 🧠 Pytest を使用した Page Object モデル pages/login_page.py class LoginPage: def __init__(self, driver): pass test_login.py def test_login_success(driver): 🏷️ Pytest マーカー(強力な機能) @pytest.mark.smoke 特定のテストを実行: pytest -m smoke 📊 HTML リポート(CI 互換性) pip install pytest-html pytest --html=report.html

Original Content

If you’re a QA engineer stepping into automation — or an SDET who just needs a quick refresher — this cheat sheet covers the Selenium + Python essentials you actually use at work. No theory overload. No outdated patterns. Just practical examples you can copy, adapt, and scale. Thanks for reading! Subscribe for free to receive new posts and support my work. ✅ Setup & Installation Install Selenium: pip install selenium Make sure the correct browser driver is available in your system PATH: Chrome → chromedriver Firefox → geckodriver Edge → msedgedriver 🚀 Starting the Browser Launch Chrome driver = webdriver.Chrome() driver.get(”https://example.com”) Headless Mode (Recommended for CI) from selenium.webdriver.chrome.options import Options options = Options() options.add_argument(”--headless”) options.add_argument(”--window-size=1920,1080”) driver = webdriver.Chrome(options=options) 🔍 Locating Elements (The Most Important Skill) Always prioritize reliable, stable locators. from selenium.webdriver.common.by import By Preferred Locators driver.find_element(By.NAME, “email”) driver.find_element(By.CSS_SELECTOR, “.login-button”) driver.find_element(By.CSS_SELECTOR, “input[type=’password’]”) XPath (Use Sparingly) driver.find_element(By.XPATH, “//button[text()=’Login’]”) ✅ Tip: If your tests break often, your locators are probably the problem. ✍️ User Interactions element.click() user@test.com”) ⏳ Waiting for Elements (Non‑Negotiable) Explicit Waits (Best Practice) Common conditions: EC.presence_of_element_located ❌ Avoid time.sleep() — it causes flaky tests. 📄 Forms & Dropdowns from selenium.webdriver.support.ui import Select 🔔 Alerts, Frames & Windows Alerts alert = driver.switch_to.alert iFrames driver.switch_to.frame(”frameName”) Multiple Tabs 📜 Scrolling & JavaScript driver.execute_script(”window.scrollTo(0, document.body.scrollHeight)”) 📷 Screenshots (For Failing Tests) driver.save_screenshot(”failure.png”) 🧹 Cleanup 🚀 Selenium + Pytest: Real‑World Automation Setup pip install pytest 🧱 Project Structure (Recommended) 🔁 Pytest WebDriver Fixture conftest.py @pytest.fixture ✅ Automatically manages setup & teardown 🧪 Writing a Test def test_valid_login(driver): https://example.com/login”) 🧠 Using Page Object Model with Pytest pages/login_page.py class LoginPage: init(self, driver): test_login.py def test_login_success(driver): 🏷️ Pytest Markers (Power Feature) @pytest.mark.smoke Run specific tests: pytest -m smoke 📊 HTML Reports (CI‑Friendly) pip install pytest-html pytest --html=report.html