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:

14
17
<details class="mt-3">
  <summary>Show full example</summary>
  <div class="pt-2">
    <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">package</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">dev.selenium.drivers</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline">

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

<div class="text-end pb-2 mt-2">
  <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/java/src/test/java/dev/selenium/drivers/ServiceTest.java#L15-L16" target="_blank">
    <i class="fas fa-external-link-alt pl-2"></i>
    <strong>View full example on GitHub</strong>
  </a>
</div>

Note: Java Service classes only allow values to be set during construction with a Builder pattern.

Selenium v4.11

4
7
<details class="mt-3">
  <summary>Show full example</summary>
  <div class="pt-2">
    <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-py" data-lang="py"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">from</span> <span style="color:#000">selenium</span> <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">webdriver</span>

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

<div class="text-end pb-2 mt-2">
  <a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full/examples/python/tests/drivers/test_service.py#L5-L6" target="_blank">
    <i class="fas fa-external-link-alt pl-2"></i>
    <strong>View full example on GitHub</strong>
  </a>
</div>