Chrome固有の機能
デフォルトでは、Selenium 4 は Chrome v75 以降と互換性があります。 Chromeブラウザのバージョンと chromedriverのバージョンは、メジャーバージョンと一致する必要があることに注意してください。
Options
全てのブラウザに共通のCapabilityについては、オプション ページで説明しています。
Chrome に固有のCapabilityは、Google のCapabilities & ChromeOptionsページにあります。
基本的な定義済みオプションを使用してChromeセッションを開始すると、次のようになります。
ChromeOptions options = new ChromeOptions();
driver = new ChromeDriver(options);
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
var options = new ChromeOptions();
driver = new ChromeDriver(options);
options = Selenium::WebDriver::Options.chrome
@driver = Selenium::WebDriver.for :chrome, options: options
const Options = new Chrome.Options();
let driver = await env
.builder()
.setChromeOptions(Options)
.build();
さまざまなCapabilityを備えた一般的な使用例をいくつか示します。
引数
The args
parameter is for a list of command line switches to be used when starting the browser.
There are two excellent resources for investigating these arguments:
Commonly used args include --start-maximized
, --headless=new
and --user-data-dir=...
Add an argument to options:
options.addArguments("--start-maximized");
options.add_argument("--start-maximized")
options.AddArgument("--start-maximized");
options.args << '--start-maximized'
let driver = await env
.builder()
.setChromeOptions(options.addArguments('--headless=new'))
.build();
指定したロケーションでブラウザを起動する
binary
パラメーターは、使用するブラウザの別のロケーションのパスを取ります。
このパラメーターを使用すると、chromedriver を使用して、さまざまな Chromium ベースのブラウザを駆動できます。
オプションにブラウザのロケーションを追加します。
options.setBinary(getChromeLocation());
options.binary_location = chrome_bin
options.BinaryLocation = GetChromeLocation();
options.binary = chrome_location
let driver = await env
.builder()
.setChromeOptions(options.setChromeBinaryPath(`Path to chrome binary`))
.build();
拡張機能を追加する
extensions
パラメーターはcrxファイルを受け入れます
The extensions
parameter accepts crx files. As for unpacked directories,
please use the load-extension
argument instead, as mentioned in
this post.
オプションに拡張機能を追加します。
options.addExtensions(extensionFilePath);
options.add_extension(extension_file_path)
options.AddExtension(extensionFilePath);
options.add_extension(extension_file_path)
const options = new Chrome.Options();
let driver = await env
.builder()
.setChromeOptions(options.addExtensions(['./test/resources/extensions/webextensions-selenium-example.crx']))
.build();
ブラウザを開いたままにする
detach
パラメータをtrueに設定すると、ドライバープロセスが終了した後もブラウザを開いたままにできます。
Note: This is already the default behavior in Java.
options.add_experimental_option("detach", True)
Note: This is already the default behavior in .NET.
options.detach = true
let driver = await env
.builder()
.setChromeOptions(options.detachDriver(true))
.build();
引数を除外する
Chrome はさまざまな引数を追加します。
これらの引数を追加したくない場合は、それらを excludeSwitches
に渡します。
一般的な例は、ポップアップブロッカーをオンに設定することです。
A full list of default arguments can be parsed from the Chromium Source Code
オプションに除外された引数を設定します。
options.setExperimentalOption("excludeSwitches", List.of("disable-popup-blocking"));
options.add_experimental_option('excludeSwitches', ['disable-popup-blocking'])
options.AddExcludedArgument("disable-popup-blocking");
options.exclude_switches << 'disable-popup-blocking'
let driver = await env
.builder()
.setChromeOptions(options.excludeSwitches('enable-automation'))
.build();
Service
Examples for creating a default Service object, and for setting driver location and port can be found on the Driver Service page.
Log output
Getting driver logs can be helpful for debugging issues. The Service class lets you direct where the logs will go. Logging output is ignored unless the user directs it somewhere.
File output
To change the logging output to save to a specific file:
ChromeDriverService service =
new ChromeDriverService.Builder().withLogFile(logLocation).build();
Note: Java also allows setting file output by System Property:
Property key: ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY
Property value: String representing path to log file
service.LogPath = GetLogLocation();
Console output
To change the logging output to display in the console as STDOUT:
ChromeDriverService service =
new ChromeDriverService.Builder().withLogOutput(System.out).build();
Note: Java also allows setting console output by System Property;
Property key: ChromeDriverService.CHROME_DRIVER_LOG_PROPERTY
Property value: DriverService.LOG_STDOUT
or DriverService.LOG_STDERR
service = webdriver.ChromeService(log_output=subprocess.STDOUT)
$stdout
and $stderr
are both valid values
service.log = $stdout
Log level
There are 6 available log levels: ALL
, DEBUG
, INFO
, WARNING
, SEVERE
, and OFF
.
Note that --verbose
is equivalent to --log-level=ALL
and --silent
is equivalent to --log-level=OFF
,
so this example is just setting the log level generically:
ChromeDriverService service =
new ChromeDriverService.Builder().withLogLevel(ChromiumDriverLogLevel.DEBUG).build();
Note: Java also allows setting log level by System Property:
Property key: ChromeDriverService.CHROME_DRIVER_LOG_LEVEL_PROPERTY
Property value: String representation of ChromiumDriverLogLevel
enum
service = webdriver.ChromeService(service_args=['--log-level=DEBUG'], log_output=subprocess.STDOUT)
Log file features
There are 2 features that are only available when logging to a file:
- append log
- readable timestamps
To use them, you need to also explicitly specify the log path and log level. The log output will be managed by the driver, not the process, so minor differences may be seen.
ChromeDriverService service =
new ChromeDriverService.Builder().withAppendLog(true).withReadableTimestamp(true).build();
Note: Java also allows toggling these features by System Property:
Property keys: ChromeDriverService.CHROME_DRIVER_APPEND_LOG_PROPERTY
and ChromeDriverService.CHROME_DRIVER_READABLE_TIMESTAMP
Property value: "true"
or "false"
service = webdriver.ChromeService(service_args=['--append-log', '--readable-timestamp'], log_output=log_path)
service.args << '--append-log'
service.args << '--readable-timestamp'
Disabling build check
Chromedriver and Chrome browser versions should match, and if they don’t the driver will error. If you disable the build check, you can force the driver to be used with any version of Chrome. Note that this is an unsupported feature, and bugs will not be investigated.
ChromeDriverService service =
new ChromeDriverService.Builder().withBuildCheckDisabled(true).build();
Note: Java also allows disabling build checks by System Property:
Property key: ChromeDriverService.CHROME_DRIVER_DISABLE_BUILD_CHECK
Property value: "true"
or "false"
service = webdriver.ChromeService(service_args=['--disable-build-check'], log_output=subprocess.STDOUT)
service.DisableBuildCheck = true;
Special Features
キャスティング
タブの共有など、Chrome Castデバイスを操作できます。
sinks = @driver.cast_sinks
unless sinks.empty?
device_name = sinks.first['name']
@driver.start_cast_tab_mirroring(device_name)
expect { @driver.stop_casting(device_name) }.not_to raise_exception
end
ネットワークの状態
さまざまなネットワークの状態をシミュレートできます。
The following examples are for local webdrivers. For remote webdrivers, please refer to the Remote WebDriver page.
@driver.network_conditions = {offline: false, latency: 100, throughput: 200}
ログ
logs = @driver.logs.get(:browser)
パーミッション
@driver.add_permission('camera', 'denied')
@driver.add_permissions('clipboard-read' => 'denied', 'clipboard-write' => 'prompt')
デベロッパー ツール
Chromeデベロッパーツールの使用に関する詳細については、[Chromeデベロッパー ツール] セクションを参照してください。