ドライバーサービスクラス
サービスクラスは、ドライバーの起動と停止を管理するためのものです。リモートWebDriverセッションでは使用できません。
サービスクラスを使用すると、ドライバーに関する情報(場所や使用するポートなど)を指定できます。また、コマンドラインに渡される引数を指定することもできます。便利な引数のほとんどは、ログに関連しています。
デフォルトサービスインスタンス
デフォルトサービスインスタンスを使用してドライバーを起動するには:
    ChromeDriverService service = new ChromeDriverService.Builder().build();
    driver = new ChromeDriver(service);examples/java/src/test/java/dev/selenium/drivers/ServiceTest.java
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()
    driver = webdriver.Chrome(service=service)examples/python/tests/drivers/test_service.py
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();
            driver = new ChromeDriver(service);examples/dotnet/SeleniumDocs/Drivers/ServiceTest.cs
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 = Selenium::WebDriver::Service.chrome
    @driver = Selenium::WebDriver.for :chrome, service: serviceexamples/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.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
    user_data_dir = Dir.mktmpdir('chrome-profile-')
    options = Selenium::WebDriver::Options.chrome(binary: browser_path)
    options.add_argument("--user-data-dir=#{user_data_dir}")
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    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
ドライバーの場所
注意: Selenium 4.6以上を使用している場合、ドライバーの場所を設定する必要はありません。Seleniumを更新できない場合や、特別な使用ケースがある場合は、ドライバーの場所を指定する方法は次のとおりです:
    ChromeDriverService service =
        new ChromeDriverService.Builder().usingDriverExecutable(driverPath).build();examples/java/src/test/java/dev/selenium/drivers/ServiceTest.java
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)examples/python/tests/drivers/test_service.py
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));examples/dotnet/SeleniumDocs/Drivers/ServiceTest.cs
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_pathexamples/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.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
    user_data_dir = Dir.mktmpdir('chrome-profile-')
    options = Selenium::WebDriver::Options.chrome(binary: browser_path)
    options.add_argument("--user-data-dir=#{user_data_dir}")
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    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
ドライバーのポート
ドライバーを特定のポートで実行したい場合は、次のように指定できます:
    ChromeDriverService service = new ChromeDriverService.Builder().usingPort(1234).build();examples/java/src/test/java/dev/selenium/drivers/ServiceTest.java
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)examples/python/tests/drivers/test_service.py
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;examples/dotnet/SeleniumDocs/Drivers/ServiceTest.cs
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 = 1234examples/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.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
    user_data_dir = Dir.mktmpdir('chrome-profile-')
    options = Selenium::WebDriver::Options.chrome(binary: browser_path)
    options.add_argument("--user-data-dir=#{user_data_dir}")
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    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
ログ記録
ログ記録機能はブラウザによって異なります。ほとんどのブラウザでは、ログの場所とレベルを指定できます。各ブラウザのページを確認してください:
最終更新 May 28, 2025: Update dependency rspec to v3.13.1 (#2320) (38997fb397)




