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:
ChromeDriverService service = new ChromeDriverService.Builder().build();
driver = new ChromeDriver(service);
Show full example
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.io.File;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.service.DriverFinder;
public class ServiceTest extends BaseTest {
@Test
public void defaultService() {
ChromeDriverService service = new ChromeDriverService.Builder().build();
driver = new ChromeDriver(service);
}
@Test
public void setDriverLocation() {
setBinaryPaths();
ChromeOptions options = getDefaultChromeOptions();
options.setBinary(browserPath);
ChromeDriverService service =
new ChromeDriverService.Builder().usingDriverExecutable(driverPath).build();
driver = new ChromeDriver(service, options);
}
@Test
public void setPort() {
ChromeDriverService service = new ChromeDriverService.Builder().usingPort(1234).build();
driver = new ChromeDriver(service);
}
private void setBinaryPaths() {
ChromeOptions options = getDefaultChromeOptions();
options.setBrowserVersion("stable");
DriverFinder finder = new DriverFinder(ChromeDriverService.createDefaultService(), options);
driverPath = new File(finder.getDriverPath());
browserPath = new File(finder.getBrowserPath());
}
}
Note: Java Service classes only allow values to be set during construction with a Builder pattern.
service = webdriver.ChromeService()
driver = webdriver.Chrome(service=service)
Show full example
from selenium import webdriver
def test_basic_service():
service = webdriver.ChromeService()
driver = webdriver.Chrome(service=service)
driver.quit()
def test_driver_location(chromedriver_bin, chrome_bin):
options = get_default_chrome_options()
options.binary_location = chrome_bin
service = webdriver.ChromeService(executable_path=chromedriver_bin)
driver = webdriver.Chrome(service=service, options=options)
driver.quit()
def test_driver_port():
service = webdriver.ChromeService(port=1234)
driver = webdriver.Chrome(service=service)
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
Note: Python Service classes only allow values to be set as arguments to the constructor.
var service = ChromeDriverService.CreateDefaultService();
driver = new ChromeDriver(service);
Show full example
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using SeleniumDocs.TestSupport;
namespace SeleniumDocs.Drivers
{
[TestClass]
public class ServiceTest : BaseTest
{
[TestMethod]
public void BasicService()
{
var service = ChromeDriverService.CreateDefaultService();
driver = new ChromeDriver(service);
}
[TestMethodCustom]
[EnabledOnOs("OSX")]
public void DriverLocation()
{
var options = GetLatestChromeOptions();
var service = ChromeDriverService.CreateDefaultService(GetDriverLocation(options));
driver = new ChromeDriver(service, options);
}
[TestMethod]
public void DriverPort()
{
var service = ChromeDriverService.CreateDefaultService();
service.Port = 1234;
driver = new ChromeDriver(service);
}
private static string GetDriverLocation(ChromeOptions options)
{
return new DriverFinder(options).GetDriverPath();
}
private static ChromeOptions GetLatestChromeOptions()
{
return new ChromeOptions
{
BrowserVersion = "stable"
};
}
}
}
Note: .NET Service classes allow values to be set as properties.
service = Selenium::WebDriver::Service.chrome
@driver = Selenium::WebDriver.for :chrome, service: service
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Service' do
let(:file_name) { File.expand_path('driver.log') }
let(:driver_path) { ENV.fetch('CHROMEDRIVER_BIN', nil) }
let(:browser_path) { ENV.fetch('CHROME_BIN', nil) }
before { driver_finder }
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
options = Selenium::WebDriver::Options.chrome(binary: browser_path)
service = Selenium::WebDriver::Service.chrome
service.executable_path = driver_path
@driver = Selenium::WebDriver.for :chrome, service: service, options: options
end
it 'specifies driver port' do
service = Selenium::WebDriver::Service.chrome
service.port = 1234
@driver = Selenium::WebDriver.for :chrome, service: service
end
def driver_finder
options = Selenium::WebDriver::Options.chrome(browser_version: 'stable')
service = Selenium::WebDriver::Service.chrome
finder = Selenium::WebDriver::DriverFinder.new(options, service)
ENV['CHROMEDRIVER_BIN'] = finder.driver_path
ENV['CHROME_BIN'] = finder.browser_path
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:
ChromeDriverService service =
new ChromeDriverService.Builder().usingDriverExecutable(driverPath).build();
Show full example
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.io.File;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.service.DriverFinder;
public class ServiceTest extends BaseTest {
@Test
public void defaultService() {
ChromeDriverService service = new ChromeDriverService.Builder().build();
driver = new ChromeDriver(service);
}
@Test
public void setDriverLocation() {
setBinaryPaths();
ChromeOptions options = getDefaultChromeOptions();
options.setBinary(browserPath);
ChromeDriverService service =
new ChromeDriverService.Builder().usingDriverExecutable(driverPath).build();
driver = new ChromeDriver(service, options);
}
@Test
public void setPort() {
ChromeDriverService service = new ChromeDriverService.Builder().usingPort(1234).build();
driver = new ChromeDriver(service);
}
private void setBinaryPaths() {
ChromeOptions options = getDefaultChromeOptions();
options.setBrowserVersion("stable");
DriverFinder finder = new DriverFinder(ChromeDriverService.createDefaultService(), options);
driverPath = new File(finder.getDriverPath());
browserPath = new File(finder.getBrowserPath());
}
}
service = webdriver.ChromeService(executable_path=chromedriver_bin)
Show full example
from selenium import webdriver
def test_basic_service():
service = webdriver.ChromeService()
driver = webdriver.Chrome(service=service)
driver.quit()
def test_driver_location(chromedriver_bin, chrome_bin):
options = get_default_chrome_options()
options.binary_location = chrome_bin
service = webdriver.ChromeService(executable_path=chromedriver_bin)
driver = webdriver.Chrome(service=service, options=options)
driver.quit()
def test_driver_port():
service = webdriver.ChromeService(port=1234)
driver = webdriver.Chrome(service=service)
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
var service = ChromeDriverService.CreateDefaultService(GetDriverLocation(options));
Show full example
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using SeleniumDocs.TestSupport;
namespace SeleniumDocs.Drivers
{
[TestClass]
public class ServiceTest : BaseTest
{
[TestMethod]
public void BasicService()
{
var service = ChromeDriverService.CreateDefaultService();
driver = new ChromeDriver(service);
}
[TestMethodCustom]
[EnabledOnOs("OSX")]
public void DriverLocation()
{
var options = GetLatestChromeOptions();
var service = ChromeDriverService.CreateDefaultService(GetDriverLocation(options));
driver = new ChromeDriver(service, options);
}
[TestMethod]
public void DriverPort()
{
var service = ChromeDriverService.CreateDefaultService();
service.Port = 1234;
driver = new ChromeDriver(service);
}
private static string GetDriverLocation(ChromeOptions options)
{
return new DriverFinder(options).GetDriverPath();
}
private static ChromeOptions GetLatestChromeOptions()
{
return new ChromeOptions
{
BrowserVersion = "stable"
};
}
}
}
service.executable_path = driver_path
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Service' do
let(:file_name) { File.expand_path('driver.log') }
let(:driver_path) { ENV.fetch('CHROMEDRIVER_BIN', nil) }
let(:browser_path) { ENV.fetch('CHROME_BIN', nil) }
before { driver_finder }
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
options = Selenium::WebDriver::Options.chrome(binary: browser_path)
service = Selenium::WebDriver::Service.chrome
service.executable_path = driver_path
@driver = Selenium::WebDriver.for :chrome, service: service, options: options
end
it 'specifies driver port' do
service = Selenium::WebDriver::Service.chrome
service.port = 1234
@driver = Selenium::WebDriver.for :chrome, service: service
end
def driver_finder
options = Selenium::WebDriver::Options.chrome(browser_version: 'stable')
service = Selenium::WebDriver::Service.chrome
finder = Selenium::WebDriver::DriverFinder.new(options, service)
ENV['CHROMEDRIVER_BIN'] = finder.driver_path
ENV['CHROME_BIN'] = finder.browser_path
end
end
Driver port
If you want the driver to run on a specific port, you may specify it as follows:
ChromeDriverService service = new ChromeDriverService.Builder().usingPort(1234).build();
Show full example
package dev.selenium.drivers;
import dev.selenium.BaseTest;
import java.io.File;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.service.DriverFinder;
public class ServiceTest extends BaseTest {
@Test
public void defaultService() {
ChromeDriverService service = new ChromeDriverService.Builder().build();
driver = new ChromeDriver(service);
}
@Test
public void setDriverLocation() {
setBinaryPaths();
ChromeOptions options = getDefaultChromeOptions();
options.setBinary(browserPath);
ChromeDriverService service =
new ChromeDriverService.Builder().usingDriverExecutable(driverPath).build();
driver = new ChromeDriver(service, options);
}
@Test
public void setPort() {
ChromeDriverService service = new ChromeDriverService.Builder().usingPort(1234).build();
driver = new ChromeDriver(service);
}
private void setBinaryPaths() {
ChromeOptions options = getDefaultChromeOptions();
options.setBrowserVersion("stable");
DriverFinder finder = new DriverFinder(ChromeDriverService.createDefaultService(), options);
driverPath = new File(finder.getDriverPath());
browserPath = new File(finder.getBrowserPath());
}
}
service = webdriver.ChromeService(port=1234)
Show full example
from selenium import webdriver
def test_basic_service():
service = webdriver.ChromeService()
driver = webdriver.Chrome(service=service)
driver.quit()
def test_driver_location(chromedriver_bin, chrome_bin):
options = get_default_chrome_options()
options.binary_location = chrome_bin
service = webdriver.ChromeService(executable_path=chromedriver_bin)
driver = webdriver.Chrome(service=service, options=options)
driver.quit()
def test_driver_port():
service = webdriver.ChromeService(port=1234)
driver = webdriver.Chrome(service=service)
driver.quit()
def get_default_chrome_options():
options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
return options
service.Port = 1234;
Show full example
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using SeleniumDocs.TestSupport;
namespace SeleniumDocs.Drivers
{
[TestClass]
public class ServiceTest : BaseTest
{
[TestMethod]
public void BasicService()
{
var service = ChromeDriverService.CreateDefaultService();
driver = new ChromeDriver(service);
}
[TestMethodCustom]
[EnabledOnOs("OSX")]
public void DriverLocation()
{
var options = GetLatestChromeOptions();
var service = ChromeDriverService.CreateDefaultService(GetDriverLocation(options));
driver = new ChromeDriver(service, options);
}
[TestMethod]
public void DriverPort()
{
var service = ChromeDriverService.CreateDefaultService();
service.Port = 1234;
driver = new ChromeDriver(service);
}
private static string GetDriverLocation(ChromeOptions options)
{
return new DriverFinder(options).GetDriverPath();
}
private static ChromeOptions GetLatestChromeOptions()
{
return new ChromeOptions
{
BrowserVersion = "stable"
};
}
}
}
service.port = 1234
Show full example
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Service' do
let(:file_name) { File.expand_path('driver.log') }
let(:driver_path) { ENV.fetch('CHROMEDRIVER_BIN', nil) }
let(:browser_path) { ENV.fetch('CHROME_BIN', nil) }
before { driver_finder }
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
options = Selenium::WebDriver::Options.chrome(binary: browser_path)
service = Selenium::WebDriver::Service.chrome
service.executable_path = driver_path
@driver = Selenium::WebDriver.for :chrome, service: service, options: options
end
it 'specifies driver port' do
service = Selenium::WebDriver::Service.chrome
service.port = 1234
@driver = Selenium::WebDriver.for :chrome, service: service
end
def driver_finder
options = Selenium::WebDriver::Options.chrome(browser_version: 'stable')
service = Selenium::WebDriver::Service.chrome
finder = Selenium::WebDriver::DriverFinder.new(options, service)
ENV['CHROMEDRIVER_BIN'] = finder.driver_path
ENV['CHROME_BIN'] = finder.browser_path
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: