Driver Service Class
服务类用于管理驱动程序的启动和停止. They can not 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);
service = ChromeService()
driver = webdriver.Chrome(service=service)
var service = FirefoxDriverService.CreateDefaultService();
driver = new FirefoxDriver(service);
service = Selenium::WebDriver::Service.chrome
@driver = Selenium::WebDriver.for :chrome, service: service
Driver location
Note: If you are using Selenium 4.6 or greater, you shouldn’t need to set a driver location. If you can not update Selenium or have an advanced use case here is how to specify the driver location:
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(driverLocation)
.build();
service = ChromeService(executable_path=driver_path)
var service = ChromeDriverService.CreateDefaultService(path);
service.executable_path = driver_path
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();
service = ChromeService(port=1234)
service.Port = 1234;
service.port = 1234
Setting log output
Getting driver logs can be helpful for debugging various issues. The Service class lets you direct where the logs will go
Currently, the default behavior for logging varies by language (and sometimes browser):
Logging output defaults to STDERR
Logging output defaults to none, except for Firefox, which defaults to “geckodriver.log”
Logging output defaults to none
Logging output defaults to none, unless the WebDriver logger level is set to :debug
,
in which case the driver logging is sent to the same output as the Selenium logging.
Logging output defaults to STDERR
File output
To change the logging output to save to a specific file:
ChromeDriverService service = new ChromeDriverService.Builder()
.withLogFile(logLocation)
.build();
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
logLocation.getAbsolutePath());
service = ChromeService(log_path=log_path)
service.LogPath = file;
service.log = file_name
Console output
To change the logging output to display in the console as STDOUT:
Java allows you to set this by method:
ChromeDriverService service = new ChromeDriverService.Builder()
.withLogOutput(System.out)
.build();
Or by System Property:
System.setProperty(ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY,
logLocation.getAbsolutePath());
This is not yet supported in Python
This is not yet supported in .NET
service.log = $stdout