開始使用 ChromeDriver

本頁說明如何開始使用 ChromeDriver 在電腦 (Windows/Mac/Linux) 上測試您的網站。您也可以參閱「Android 入門指南」或「ChromeOS 入門指南」。

設定

ChromeDriver 是 Selenium WebDriver 用來控制 Chrome 的獨立執行檔,它是由 Chromium 團隊負責維護,由 WebDriver 貢獻者提供協助。如果您不熟悉 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 伺服器時啟動 ChromeDriver 伺服器程序,並在呼叫關閉時終止程序。對於每次測試建立 ChromeDriver 執行個體的大型測試套件,這可能導致大量測試套件耗費大量時間。解決方法有兩種:

  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();
  }
}