Driver Service Class
The Service classes are for managing the starting and stopping of local drivers. They cannot be used with a Remote WebDriver session.
Service classes allow you to specify information about the driver, like location and which port to use. They also let you specify what arguments get passed to the command line. Most of the useful arguments are related to logging.
Default Service instance
To start a driver with a default service instance:
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();
}
}
Note: Java Service classes only allow values to be set during construction with a Builder pattern.
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()
Note: Python Service classes only allow values to be set as arguments to the constructor.
[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"));
}
}
}
Note: .NET Service classes allow values to be set as properties.
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
Note: Ruby Service classes allow values to be set either as arguments in the constructor or as attributes.
Driver location
Note: If you are using Selenium 4.6 or greater, you shouldn’t need to set a driver location. If you cannot update Selenium or have an advanced use case, here is how to specify the driver location:
@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
Driver port
If you want the driver to run on a specific port, you may specify it as follows:
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
Logging
Logging functionality varies between browsers. Most browsers allow you to specify location and level of logs. Take a look at the respective browser page: