ChromeDriver のスタートガイド

このページでは、ChromeDriver を使用してパソコン(Windows/Mac/Linux)でウェブサイトをテストする方法について説明します。Android のスタートガイドまたは ChromeOS のスタートガイドもご覧ください。

設定

ChromeDriver は、Selenium WebDriver が Chrome の制御に使用する独立した実行可能ファイルです。このソフトウェアは、WebDriver のコントリビューターの助けを借りて Chromium チームによって管理されています。Selenium WebDriver になじみがない場合は、Selenium のサイトをご覧ください。

ChromeDriver で実行するためのテストをセットアップする手順は次のとおりです。

  • Chromium または Google Chrome が認識済みの場所にインストールされていることを確認する
  • お使いのプラットフォーム用の ChromeDriver バイナリを、このサイトのダウンロード セクションからダウンロードします。
  • WebDriver がダウンロードした ChromeDriver 実行可能ファイルを検出できるようにする

次のいずれかの手順をお試しください。

  1. PATH 環境変数に ChromeDriver の場所を含める
  2. Java のみ)webdriver.chrome.driver システム プロパティを使用して場所を指定します(下記のサンプルを参照)。
  3. Python のみ)webdriver.Chrome のインスタンス化時に ChromeDriver のパスを含める(下記のサンプルを参照)

サンプルテスト

Java:

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.junit.Test;
public class GettingStarted {   
@Test   
public void testGoogleSearch() throws InterruptedException {
  // Optional. If not specified, WebDriver searches the PATH for chromedriver.
  // System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
  // WebDriver driver = new ChromeDriver();
  driver.get("http://www.google.com/"); 
  Thread.sleep(5000);  // Let the user actually see something!
  WebElement searchBox = driver.findElement(By.name("q"));
  searchBox.sendKeys("ChromeDriver");
  searchBox.submit(); 
  Thread.sleep(5000);  // Let the user actually see something!
  driver.quit();  
 }
}

Python:

import time
from selenium import webdriver

driver = webdriver.Chrome('/path/to/chromedriver')  # Optional argument, if not specified will search path.
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('ChromeDriver')
search_box.submit()
time.sleep(5) # Let the user actually see something!
driver.quit()

ChromeDriver の存続期間を制御する

ChromeDriver クラスは、作成時に ChromeDriver サーバー プロセスを開始し、quit が呼び出されると終了します。テストごとに ChromeDriver インスタンスが作成される大規模なテストスイートでは、かなりの時間が浪費される可能性があります。この問題を解決するには、次の 2 つの方法があります。

  1. ChromeDriverService を使用する。これはほとんどの言語で利用でき、ユーザーは自分で ChromeDriver サーバーを起動または停止できます。Java の例(JUnit 4 の場合)をご覧ください。
import java.io.*;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.remote.*;
public class GettingStartedWithService {
  private static ChromeDriverService service;
  private WebDriver driver;
  @BeforeClass
  public static void createAndStartService() throws IOException {
      service = new ChromeDriverService.Builder()
              .usingDriverExecutable(new File("/path/to/chromedriver"))
              .usingAnyFreePort()
              .build();
      service.start();
  }
  
  @AfterClass   
  public static void stopService() {
    service.stop();
  }

  @Before   
  public void createDriver() {
    driver = new RemoteWebDriver(service.getUrl(), new ChromeOptions());
  }

  @After   public void quitDriver() {
    driver.quit();
  }

  @Test   
  public void testGoogleSearch() {
    driver.get("http://www.google.com");
    // rest of the test...
  }
}

Python:

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service('/path/to/chromedriver')
service.start()
driver = webdriver.Remote(service.service_url)
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
driver.quit()
  1. テストを実行する前に ChromeDriver サーバーを個別に起動し、Remote WebDriver を使用して接続します。

端末:

$ ./chromedriver
Starting ChromeDriver
76.0.3809.68 (...) on port 9515
...

Java:

import java.net.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.remote.*;  

public class GettingStartedRemote {

  public static void main(String[] args) throws MalformedURLException {
    WebDriver driver = new RemoteWebDriver(
        new URL("http://127.0.0.1:9515"),
        new ChromeOptions());
    driver.get("http://www.google.com");
    driver.quit();
  }
}