ドライバーサービスクラス

サービスクラスは、ドライバーの起動と停止を管理するためのものです。リモートWebDriverセッションでは使用できません。

サービスクラスを使用すると、ドライバーに関する情報(場所や使用するポートなど)を指定できます。また、コマンドラインに渡される引数を指定することもできます。便利な引数のほとんどは、ログに関連しています。

デフォルトサービスインスタンス

デフォルトサービスインスタンスを使用してドライバーを起動するには:

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

Selenium v4.11

    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
            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"
            };
        }
    }
}
    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

ドライバーの場所

注意: Selenium 4.6以上を使用している場合、ドライバーの場所を設定する必要はありません。Seleniumを更新できない場合や、特別な使用ケースがある場合は、ドライバーの場所を指定する方法は次のとおりです:

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

Selenium v4.11

    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

Selenium v4.9

            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"
            };
        }
    }
}

Selenium v4.8

    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

ドライバーのポート

ドライバーを特定のポートで実行したい場合は、次のように指定できます:

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

Selenium v4.11

    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"
            };
        }
    }
}

Selenium v4.8

    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

ログ記録

ログ記録機能はブラウザによって異なります。ほとんどのブラウザでは、ログの場所とレベルを指定できます。各ブラウザのページを確認してください: