Chrome specific functionality
默认情况下,Selenium 4与Chrome v75及更高版本兼容. 但是请注意Chrome浏览器的版本与chromedriver的主版本需要匹配.
Options
所有浏览器的通用功能请看这 Options page.
Chrome浏览器的特有功能可以在谷歌的页面找到: Capabilities & ChromeOptions
基于默认选项的Chrome浏览器会话看起来是这样:
def test_keep_browser_open():
driver = new ChromeDriver(options);
it 'basic options' do
options = Selenium::WebDriver::Options.chrome
const Options = new Chrome.Options();
let driver = await env
.builder()
.setChromeOptions(Options)
.build();
下面是一些不同功能的常见示例:
参数
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:
driver.quit()
options = Selenium::WebDriver::Options.chrome(args: ['--headless=new'])
let driver = await env
.builder()
.setChromeOptions(options.addArguments('--headless=new'))
.build();
从指定位置启动浏览器
binary
参数接收一个使用浏览器的备用路径,通过这个参数你可以使用chromedriver 去驱动各种基于Chromium 内核的浏览器.
添加一个浏览器地址到选项中:
let driver = await env
.builder()
.setChromeOptions(options.setChromeBinaryPath(`Path to chrome binary`))
.build();
添加扩展程序
extensions
参数接受crx文件. As for unpacked directories,
please use the load-extension
argument instead, as mentioned in
this post.
添加一个扩展程序到选项中:
保持浏览器的打开状态
将 detach
参数设置为true将在驱动过程结束后保持浏览器的打开状态.
添加一个布尔值到选项中:
Note: This is already the default behavior in Java.
Note: This is already the default behavior in .NET.
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.AddExcludedArgument("disable-popup-blocking");
@driver = Selenium::WebDriver.for :chrome, options: options
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:
String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
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:
String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
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
$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:
String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
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
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.
String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
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.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.
String fileContent = new String(Files.readAllBytes(getLogLocation().toPath()));
Note: Java also allows disabling build checks by System Property:
Property key: ChromeDriverService.CHROME_DRIVER_DISABLE_BUILD_CHECK
Property value: "true"
or "false"
service.DisableBuildCheck = true;
Special Features
Casting
你可以驱动 Chrome Cast 设备,包括共享选项卡
Coding Help
Check our contribution guidelines and code example formats if you’d like to help.
网络条件
您可以模拟各种网络条件.
The following examples are for local webdrivers. For remote webdrivers, please refer to the Remote WebDriver page.
Coding Help
Check our contribution guidelines and code example formats if you’d like to help.
Logs
Coding Help
Check our contribution guidelines and code example formats if you’d like to help.
Permissions
Coding Help
Check our contribution guidelines and code example formats if you’d like to help.
DevTools
See the Chrome DevTools section for more information about using Chrome DevTools