เริ่มต้นใช้งาน ChromeDriver

หน้านี้จะแสดงวิธีเริ่มใช้ ChromeDriver ในการทดสอบเว็บไซต์บนเดสก์ท็อป (Windows/Mac/Linux) คุณยังอ่านการเริ่มต้นใช้งาน Android หรือการเริ่มต้นใช้งาน ChromeOS ได้อีกด้วย

ตั้งค่า

ChromeDriver เป็นไฟล์สั่งการแยกต่างหากที่ Selenium WebDriver ใช้ควบคุม Chrome เครื่องมือนี้ดูแลโดยทีม Chromium ด้วยความช่วยเหลือจากผู้ร่วมให้ข้อมูล WebDriver หากไม่คุ้นเคยกับ Selenium WebDriver ให้ลองไปที่เว็บไซต์ Selenium

ทำตามขั้นตอนต่อไปนี้เพื่อตั้งค่าการทดสอบสำหรับการเรียกใช้ด้วย ChromeDriver

  • ตรวจสอบว่าติดตั้ง Chromium/Google Chrome ในตำแหน่งที่ระบบรู้จัก
  • ดาวน์โหลดไบนารี ChromeDriver สำหรับแพลตฟอร์มของคุณในส่วนดาวน์โหลดของเว็บไซต์นี้
  • ช่วย WebDriver ค้นหาไฟล์ปฏิบัติการ ChromeDriver ที่ดาวน์โหลดมา

ขั้นตอนเหล่านี้ควรช่วยแก้ปัญหาได้

  1. รวมตำแหน่ง ChromeDriver ในตัวแปรสภาพแวดล้อม PATH ของคุณ
  2. (Java เท่านั้น) ระบุตำแหน่งโดยใช้คุณสมบัติของระบบ webdriver.chrome.driver (ดูตัวอย่างด้านล่าง)
  3. (เฉพาะ Python) รวมเส้นทางไปยัง ChromeDriver เมื่อสร้างอินสแตนซ์ webdriver.Chrome (ดูตัวอย่างด้านล่าง)

การทดสอบตัวอย่าง

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

งูหลาม:

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 ตามการทดสอบ วิธีแก้ไขปัญหานี้มี 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...
  }
}

งูหลาม:

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