chromedriver Capabilities & ChromeOptions

Capabilities are options that you can use to customize and configure a ChromeDriver session. This page documents all ChromeDriver supported capabilities and how to use them.

There are two ways to specify capabilities. 
  1. The first is to use the ChromeOptions class.  
  2. If your client library does not have a ChromeOptions class (like the selenium ruby client), you can specify the capabilities directly as part of the DesiredCapabilities.

Using the ChromeOptions class

You can create an instance of  ChromeOptions, which has convenient methods for setting ChromeDriver-specific capabilities. You can pass the  ChromeOptions object directly into the ChromeDriver constructor:
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
ChromeDriver driver = new ChromeDriver(options);

Alternatively, you can add the options to an already existing  DesiredCapabilities object, which is useful when need to specify other WebDriver capabilities not specific to ChromeDriver.
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
// Add the WebDriver proxy capability.
Proxy proxy = new Proxy();
proxy.setHttpProxy("myhttpproxy:3337");
capabilities.setCapability("proxy", proxy);

// Add ChromeDriver-specific capabilities through ChromeOptions.
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);

Using DesiredCapabilities directly

The  ChromeOptions class uses  DesiredCapabilities underneath. To use  DesiredCapabilities directly, you need to know the name of the capability and the type of value it takes. See the full list further below.
Java
Map<String, Object> chromeOptions = new Map<String, Object>();
chromeOptions.put("binary", "/usr/lib/chromium-browser/chromium-browser");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver = new ChromeDriver(capabilities);
Ruby
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "--disable-web-security" ]})
driver = Selenium::WebDriver.for :remote, url: 'http://localhost:4444/wd/hub' desired_capabilities: caps

Common use cases

Use custom profile (also called user data directory)
By default, ChromeDriver will create a new temporary profile for each session. At times you may want to set special preferences or just use a custom profile altogether. If the former, you can use the 'chrome.prefs' capability (described later below) to specify preferences that will be applied after Chrome starts. If the latter, you can use the user-data-dir Chrome command-line switch to tell Chrome which profile to use:
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/your/custom/profile");
You can create your own custom profile by just running Chrome (on the command-line or through ChromeDriver) with the  user-data-dir switch set to some new directory. If the path doesn't exist, Chrome will create a new profile in the specified location. You can then modify the profile settings as desired, and ChromeDriver can use the profile in the future. Open  chrome://version in the browser to see what profile Chrome is using.
Start Chrome maximized
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");

Using a Chrome executable in a non-standard location
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");

Set a Chrome preference
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
options.setExperimentalOption("prefs", prefs);

List of recognized capabilities

This is a list of all the WebDriver-standard capabilities that ChromeDriver supports:

Proxy object

See http://code.google.com/p/selenium/wiki/DesiredCapabilities#Proxy_JSON_Object 

loggingPrefs object

chromeOptions object

This is a list of all the Chrome-specific desired capabilities, which all are under the  chromeOptions dictionary. 
If possible, use the  ChromeOptions class instead of specifying these directly.

 NameTypeDefault Description 
args list of strings  List of command-line arguments to use when starting Chrome. Arguments with an associated value should be separated by a '=' sign (e.g., ['start-maximized', 'user-data-dir=/tmp/temp_profile']). See here for a list of Chrome arguments.
binarystring  Path to the Chrome executable to use (on Mac OS X, this should be the actual binary, not just the app. e.g., '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome')
extensionslist of strings  A list of Chrome extensions to install on startup. Each item in the list should be a base-64 encoded packed Chrome extension (.crx)
localStatedictionary A dictionary with each entry consisting of the name of the preference and its value. These preferences are applied to the Local State file in the user data folder.
prefsdictionary A dictionary with each entry consisting of the name of the preference and its value. These preferences are only applied to the user profile in use. See the 'Preferences' file in Chrome's user data directory for examples.
detachbooleanfalse If false, Chrome will be quit when ChromeDriver is killed, regardless of whether the session is quit. If true, Chrome will only be quit if the session is quit (or closed). Note, if true, and the session is not quit, ChromeDriver cannot clean up the temporary user data directory that the running Chrome instance is using.
debuggerAddressstring  An address of a Chrome debugger server to connect to, in the form of <hostname/ip:port>, e.g. '127.0.0.1:38947'
excludeSwitcheslist of strings  List of Chrome command line switches to exclude that ChromeDriver by default passes when starting Chrome.  Do not prefix switches with --.
minidumpPath string  Directory to store Chrome minidumps . (Supported only on Linux.)
mobileEmulationdictionary A dictionary with either a value for “deviceName,” or values for “deviceMetrics” and “userAgent.” Refer to Mobile Emulation for more information.
perfLoggingPrefsdictionary  An optional dictionary that specifies performance logging preferences. See below for more information.
 windowTypes list of strings A list of window types that will appear in the list of window handles. For access to <webview> elements, include "webview" in this list.

perfLoggingPrefs object

The perfLoggingPrefs dictionary has the following format (all keys are optional):


NameTypeDefaultDescription
enableNetworkbooleantrueWhether or not to collect events from Network domain.
enablePagebooleantrueWhether or not to collect events from Page domain.
enableTimelinebooleantrue (false if tracing is enabled)Whether or not to collect events from Timeline domain. Note: when tracing is enabled, Timeline domain is implicitly disabled, unless enableTimeline is explicitly set to true.
tracingCategoriesstring(empty)A comma-separated string of Chrome tracing categories for which trace events should be collected. An unspecified or empty string disables tracing.
bufferUsageReportingIntervalpositive integer1000The requested number of milliseconds between DevTools trace buffer usage events. For example, if 1000, then once per second, DevTools will report how full the trace buffer is. If a report indicates the buffer usage is 100%, a warning will be issued.

Returned Capabilities

This is a list of all the Chrome-specific returned capabilities. (i.e., what ChromeDriver returns when you create a new session)
 
NameType Description 
chrome.chromedriverVersionstring version of ChromeDriver 
userDataDirstring path to user data directory that Chrome is using; note, this is inside a 'chrome' dictionary
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,报错`AttributeError: 'str' object has no attribute 'capabilities'`可能是由于使用了错误的ChromeDriver版本导致的。请尝试降低ChromeDriver的版本以解决此问题。 以下是一个示例,演示如何使用正确的ChromeDriver版本来解决该问题: ```python from selenium import webdriver # 指定正确的ChromeDriver路径和版本 chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') # 无界面模式 chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('--disable-extensions') chrome_options.add_argument('--disable-software-rasterizer') chrome_options.add_argument('--disable-features=VizDisplayCompositor') chrome_options.add_argument('--disable-features=NetworkService') chrome_options.add_argument('--disable-features=VizHitTestSurfaceLayer') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad') chrome_options.add_argument('--disable-features=VizHitTestDrawQuad

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值