驱动服务类
服务类用于管理驱动程序的启动和停止. 它们不能与远程 WebDriver 会话一起使用.
服务类允许您指定有关驱动程序的信息, 诸如位置和要使用的端口. 它们还允许您指定传递哪些参数到命令行. 大多数有用的参数都与日志记录有关.
默认服务实例
使用默认服务实例启动驱动程序:
import org.openqa.selenium.remote.service.DriverService;
examples/java/src/test/java/dev/selenium/drivers/ServiceTest.java
package dev.selenium.drivers;
import com.google.common.io.ByteStreams;
import dev.selenium.BaseTest;
import java.io.File;
import java.time.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverLogLevel;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chromium.ChromiumDriverLogLevel;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.remote.service.DriverService;
public class ServiceTest extends BaseTest {
private final File logLocation = new File("driver.log");
private final File driverLocation = new File(System.getenv("CHROMEWEBDRIVER") + "/chromedriver");
@AfterEach
public void quit() {
driver.quit();
}
@Test
public void defaultService() {
ChromeDriverService service = new ChromeDriverService.Builder().build();
driver = new ChromeDriver(service);
}
@Test
public void setDriverLocation() {
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(driverLocation)
.build();
driver = new ChromeDriver(service);
}
@Test
public void setPort() {
ChromeDriverService service = new ChromeDriverService.Builder()
.usingPort(1234)
.build();
driver = new ChromeDriver(service);
}
@Test
public void logsToFileWithLogOutput() {
ChromeDriverService service = new ChromeDriverService.Builder()
.withLogFile(logLocation)
.build();
driver = new ChromeDriver(service);
}
@Test
public void logsToFileProperty() {
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
logLocation.getAbsolutePath());
driver = new ChromeDriver();
}
@Test
public void logsToStdoutWithLogOutput() {
ChromeDriverService service = new ChromeDriverService.Builder()
.withLogOutput(System.out)
.build();
driver = new ChromeDriver(service);
}
@Test
public void logsToStdoutProperty() {
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
logLocation.getAbsolutePath());
driver = new ChromeDriver();
}
}
from selenium.webdriver.chrome.service import Service as ChromeService
examples/python/tests/drivers/test_service.py
import os
import subprocess
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
def test_basic_service():
service = ChromeService()
driver = webdriver.Chrome(service=service)
driver.quit()
def test_driver_location():
driver_path = os.getenv('CHROMEWEBDRIVER') + 'chromedriver'
service = ChromeService(executable_path=driver_path)
driver = webdriver.Chrome(service=service)
driver.quit()
def test_driver_port():
service = ChromeService(port=1234)
driver = webdriver.Chrome(service=service)
driver.quit()
def test_log_to_file():
log_path = 'chromedriver.log'
service = ChromeService(log_path=log_path)
driver = webdriver.Chrome(service=service)
with open(log_path, 'r') as fp:
assert "Starting ChromeDriver" in fp.readline()
driver.quit()
[TestMethod]
public void BasicService()
examples/dotnet/SeleniumDocs/Drivers/ServiceTest.cs
using System;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
namespace SeleniumDocs.Drivers
{
[TestClass]
public class ServiceTest : BaseTest
{
[TestMethod]
public void BasicService()
{
var service = FirefoxDriverService.CreateDefaultService();
driver = new FirefoxDriver(service);
}
[TestMethod]
public void DriverLocation()
{
var path = Environment.GetEnvironmentVariable("GECKOWEBDRIVER") + "/geckodriver";
var service = ChromeDriverService.CreateDefaultService(path);
driver = new ChromeDriver(service);
}
[TestMethod]
public void DriverPort()
{
var path = Environment.GetEnvironmentVariable("GECKOWEBDRIVER") + "/geckodriver";
var service = FirefoxDriverService.CreateDefaultService();
service.Port = 1234;
driver = new FirefoxDriver(service);
}
[TestMethod]
public void LogsToFile()
{
var service = ChromeDriverService.CreateDefaultService();
var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../selenium.log");
service.LogPath = file;
driver = new ChromeDriver(service);
driver.Url = "https://www.selenium.dev/";
var lines = File.ReadLines(file);
Assert.IsTrue(lines.First().Contains("Starting ChromeDriver"));
}
}
}
end
examples/ruby/spec/drivers/service_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Service' do
let(:file_name) { File.expand_path('driver.log') }
let(:driver_path) { "#{ENV['CHROMEWEBDRIVER']}/chromedriver" }
after { FileUtils.rm_f(file_name) }
it 'has default service' do
service = Selenium::WebDriver::Service.chrome
@driver = Selenium::WebDriver.for :chrome, service: service
end
it 'specifies driver location' do
service = Selenium::WebDriver::Service.chrome
service.executable_path = driver_path
@driver = Selenium::WebDriver.for :chrome, service: service
end
it 'specifies driver port' do
service = Selenium::WebDriver::Service.chrome
service.port = 1234
@driver = Selenium::WebDriver.for :chrome, service: service
end
it 'logs to file' do
service = Selenium::WebDriver::Service.chrome
service.log = file_name
@driver = Selenium::WebDriver.for :chrome, service: service
expect(File.readlines(file_name).size).to eq 4
end
it 'logs to stdout' do
service = Selenium::WebDriver::Service.chrome
service.log = $stdout
expect {
@driver = Selenium::WebDriver.for :chrome, service: service
}.to output(/Starting ChromeDriver/).to_stdout_from_any_process
end
end
驱动程序位置
注意: 如果您使用的是 Selenium 4.6 或更高版本, 则无需设置驱动程序位置. 如果您无法更新 Selenium 或有高阶用法需求, 以下是指定驱动程序位置的方法:
@Test
examples/java/src/test/java/dev/selenium/drivers/ServiceTest.java
package dev.selenium.drivers;
import com.google.common.io.ByteStreams;
import dev.selenium.BaseTest;
import java.io.File;
import java.time.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverLogLevel;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chromium.ChromiumDriverLogLevel;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.remote.service.DriverService;
public class ServiceTest extends BaseTest {
private final File logLocation = new File("driver.log");
private final File driverLocation = new File(System.getenv("CHROMEWEBDRIVER") + "/chromedriver");
@AfterEach
public void quit() {
driver.quit();
}
@Test
public void defaultService() {
ChromeDriverService service = new ChromeDriverService.Builder().build();
driver = new ChromeDriver(service);
}
@Test
public void setDriverLocation() {
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(driverLocation)
.build();
driver = new ChromeDriver(service);
}
@Test
public void setPort() {
ChromeDriverService service = new ChromeDriverService.Builder()
.usingPort(1234)
.build();
driver = new ChromeDriver(service);
}
@Test
public void logsToFileWithLogOutput() {
ChromeDriverService service = new ChromeDriverService.Builder()
.withLogFile(logLocation)
.build();
driver = new ChromeDriver(service);
}
@Test
public void logsToFileProperty() {
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
logLocation.getAbsolutePath());
driver = new ChromeDriver();
}
@Test
public void logsToStdoutWithLogOutput() {
ChromeDriverService service = new ChromeDriverService.Builder()
.withLogOutput(System.out)
.build();
driver = new ChromeDriver(service);
}
@Test
public void logsToStdoutProperty() {
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
logLocation.getAbsolutePath());
driver = new ChromeDriver();
}
}
driver_path = os.getenv('CHROMEWEBDRIVER') + 'chromedriver'
examples/python/tests/drivers/test_service.py
import os
import subprocess
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
def test_basic_service():
service = ChromeService()
driver = webdriver.Chrome(service=service)
driver.quit()
def test_driver_location():
driver_path = os.getenv('CHROMEWEBDRIVER') + 'chromedriver'
service = ChromeService(executable_path=driver_path)
driver = webdriver.Chrome(service=service)
driver.quit()
def test_driver_port():
service = ChromeService(port=1234)
driver = webdriver.Chrome(service=service)
driver.quit()
def test_log_to_file():
log_path = 'chromedriver.log'
service = ChromeService(log_path=log_path)
driver = webdriver.Chrome(service=service)
with open(log_path, 'r') as fp:
assert "Starting ChromeDriver" in fp.readline()
driver.quit()
{
examples/dotnet/SeleniumDocs/Drivers/ServiceTest.cs
using System;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
namespace SeleniumDocs.Drivers
{
[TestClass]
public class ServiceTest : BaseTest
{
[TestMethod]
public void BasicService()
{
var service = FirefoxDriverService.CreateDefaultService();
driver = new FirefoxDriver(service);
}
[TestMethod]
public void DriverLocation()
{
var path = Environment.GetEnvironmentVariable("GECKOWEBDRIVER") + "/geckodriver";
var service = ChromeDriverService.CreateDefaultService(path);
driver = new ChromeDriver(service);
}
[TestMethod]
public void DriverPort()
{
var path = Environment.GetEnvironmentVariable("GECKOWEBDRIVER") + "/geckodriver";
var service = FirefoxDriverService.CreateDefaultService();
service.Port = 1234;
driver = new FirefoxDriver(service);
}
[TestMethod]
public void LogsToFile()
{
var service = ChromeDriverService.CreateDefaultService();
var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../selenium.log");
service.LogPath = file;
driver = new ChromeDriver(service);
driver.Url = "https://www.selenium.dev/";
var lines = File.ReadLines(file);
Assert.IsTrue(lines.First().Contains("Starting ChromeDriver"));
}
}
}
examples/ruby/spec/drivers/service_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Service' do
let(:file_name) { File.expand_path('driver.log') }
let(:driver_path) { "#{ENV['CHROMEWEBDRIVER']}/chromedriver" }
after { FileUtils.rm_f(file_name) }
it 'has default service' do
service = Selenium::WebDriver::Service.chrome
@driver = Selenium::WebDriver.for :chrome, service: service
end
it 'specifies driver location' do
service = Selenium::WebDriver::Service.chrome
service.executable_path = driver_path
@driver = Selenium::WebDriver.for :chrome, service: service
end
it 'specifies driver port' do
service = Selenium::WebDriver::Service.chrome
service.port = 1234
@driver = Selenium::WebDriver.for :chrome, service: service
end
it 'logs to file' do
service = Selenium::WebDriver::Service.chrome
service.log = file_name
@driver = Selenium::WebDriver.for :chrome, service: service
expect(File.readlines(file_name).size).to eq 4
end
it 'logs to stdout' do
service = Selenium::WebDriver::Service.chrome
service.log = $stdout
expect {
@driver = Selenium::WebDriver.for :chrome, service: service
}.to output(/Starting ChromeDriver/).to_stdout_from_any_process
end
end
驱动程序端口
如果希望驱动程序在特定端口上运行, 您可以在启动时指定端口号, 如下所示:
public void setDriverLocation() {
examples/java/src/test/java/dev/selenium/drivers/ServiceTest.java
package dev.selenium.drivers;
import com.google.common.io.ByteStreams;
import dev.selenium.BaseTest;
import java.io.File;
import java.time.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverLogLevel;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chromium.ChromiumDriverLogLevel;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;
import org.openqa.selenium.remote.service.DriverService;
public class ServiceTest extends BaseTest {
private final File logLocation = new File("driver.log");
private final File driverLocation = new File(System.getenv("CHROMEWEBDRIVER") + "/chromedriver");
@AfterEach
public void quit() {
driver.quit();
}
@Test
public void defaultService() {
ChromeDriverService service = new ChromeDriverService.Builder().build();
driver = new ChromeDriver(service);
}
@Test
public void setDriverLocation() {
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(driverLocation)
.build();
driver = new ChromeDriver(service);
}
@Test
public void setPort() {
ChromeDriverService service = new ChromeDriverService.Builder()
.usingPort(1234)
.build();
driver = new ChromeDriver(service);
}
@Test
public void logsToFileWithLogOutput() {
ChromeDriverService service = new ChromeDriverService.Builder()
.withLogFile(logLocation)
.build();
driver = new ChromeDriver(service);
}
@Test
public void logsToFileProperty() {
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
logLocation.getAbsolutePath());
driver = new ChromeDriver();
}
@Test
public void logsToStdoutWithLogOutput() {
ChromeDriverService service = new ChromeDriverService.Builder()
.withLogOutput(System.out)
.build();
driver = new ChromeDriver(service);
}
@Test
public void logsToStdoutProperty() {
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
logLocation.getAbsolutePath());
driver = new ChromeDriver();
}
}
def test_driver_port():
examples/python/tests/drivers/test_service.py
import os
import subprocess
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
def test_basic_service():
service = ChromeService()
driver = webdriver.Chrome(service=service)
driver.quit()
def test_driver_location():
driver_path = os.getenv('CHROMEWEBDRIVER') + 'chromedriver'
service = ChromeService(executable_path=driver_path)
driver = webdriver.Chrome(service=service)
driver.quit()
def test_driver_port():
service = ChromeService(port=1234)
driver = webdriver.Chrome(service=service)
driver.quit()
def test_log_to_file():
log_path = 'chromedriver.log'
service = ChromeService(log_path=log_path)
driver = webdriver.Chrome(service=service)
with open(log_path, 'r') as fp:
assert "Starting ChromeDriver" in fp.readline()
driver.quit()
{
examples/dotnet/SeleniumDocs/Drivers/ServiceTest.cs
using System;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
namespace SeleniumDocs.Drivers
{
[TestClass]
public class ServiceTest : BaseTest
{
[TestMethod]
public void BasicService()
{
var service = FirefoxDriverService.CreateDefaultService();
driver = new FirefoxDriver(service);
}
[TestMethod]
public void DriverLocation()
{
var path = Environment.GetEnvironmentVariable("GECKOWEBDRIVER") + "/geckodriver";
var service = ChromeDriverService.CreateDefaultService(path);
driver = new ChromeDriver(service);
}
[TestMethod]
public void DriverPort()
{
var path = Environment.GetEnvironmentVariable("GECKOWEBDRIVER") + "/geckodriver";
var service = FirefoxDriverService.CreateDefaultService();
service.Port = 1234;
driver = new FirefoxDriver(service);
}
[TestMethod]
public void LogsToFile()
{
var service = ChromeDriverService.CreateDefaultService();
var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../selenium.log");
service.LogPath = file;
driver = new ChromeDriver(service);
driver.Url = "https://www.selenium.dev/";
var lines = File.ReadLines(file);
Assert.IsTrue(lines.First().Contains("Starting ChromeDriver"));
}
}
}
examples/ruby/spec/drivers/service_spec.rb
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Service' do
let(:file_name) { File.expand_path('driver.log') }
let(:driver_path) { "#{ENV['CHROMEWEBDRIVER']}/chromedriver" }
after { FileUtils.rm_f(file_name) }
it 'has default service' do
service = Selenium::WebDriver::Service.chrome
@driver = Selenium::WebDriver.for :chrome, service: service
end
it 'specifies driver location' do
service = Selenium::WebDriver::Service.chrome
service.executable_path = driver_path
@driver = Selenium::WebDriver.for :chrome, service: service
end
it 'specifies driver port' do
service = Selenium::WebDriver::Service.chrome
service.port = 1234
@driver = Selenium::WebDriver.for :chrome, service: service
end
it 'logs to file' do
service = Selenium::WebDriver::Service.chrome
service.log = file_name
@driver = Selenium::WebDriver.for :chrome, service: service
expect(File.readlines(file_name).size).to eq 4
end
it 'logs to stdout' do
service = Selenium::WebDriver::Service.chrome
service.log = $stdout
expect {
@driver = Selenium::WebDriver.for :chrome, service: service
}.to output(/Starting ChromeDriver/).to_stdout_from_any_process
end
end
日志
日志记录功能因浏览器而异. 大多数浏览器都允许您指定日志的位置和级别. 请查看相应的浏览器页面:
最后修改 May 28, 2025: Update dependency rspec to v3.13.1 (#2320) (38997fb397)