1、加载插件:
1 public static void StartChromeLoadPlugin(){
2 System.out.println("start firefox browser...");
3 System.setProperty("webdriver.chrome.driver", "files\\chromedriver.exe");
4 File file = new File ("files\\youtube.crx");
5 ChromeOptions options = new ChromeOptions();
6 options.addExtensions(file);
7 WebDriver driver = new ChromeDriver(options);
8 driver.get("http://www.baidu.com/");
9 System.out.println("start firefox browser succeed...");
10 }
System.setProperty("webdriver.chrome.driver","D:/Workspaces/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/Administrator/AppData/Local/Google/Chrome/User Data");
WebDriver driver = new ChromeDriver(options);
2.加载其他插件
Selenium操作浏览器是不加载任何配置的,网上找了半天,关于Firefox加载配置的多点,Chrome资料很少,下面是关于加载Chrome配置的方法:
一、加载所有Chrome配置
用Chrome地址栏输入chrome://version/,查看自己的“个人资料路径”,然后在浏览器启动时,调用这个配置文件,代码如下:
#coding=utf-8
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data') #设置成用户自己的数据目录
driver = webdriver.Chrome(chrome_options=option)
二、修改浏览器的User-Agent来伪装你的浏览器访问手机m站
#coding=utf-8
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('--user-agent=iphone')
driver = webdriver.Chrome(chrome_options=option)
driver.get('http://www.taobao.com/')
三、浏览器启动时安装crx扩展
#coding=utf-8
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_extension('d:\crx\AdBlock_v2.17.crx') #自己下载的crx路径
driver = webdriver.Chrome(chrome_options=option)
driver.get('http://www.taobao.com/')
可以去https://sites.google.com/a/chromium.org/chromedriver/capabilities查看更多,或者去http://stackoverflow.com/查找。
下边是收集的一些配置信息:
一些Chrome的地址栏命令(这些命令会不停的变动,所有不一定都是好用的)
在Chrome的浏览器地址栏中输入以下命令,就会返回相应的结果。这些命令包括查看内存状态,浏览器状态,网络状态,DNS服务器状态,插件缓存等等。
about:version - 显示当前版本
about:memory - 显示本机浏览器内存使用状况
about:plugins - 显示已安装插件
about:histograms - 显示历史记录
about:dns - 显示DNS状态
about:cache - 显示缓存页面
about:gpu -是否有硬件加速
about:flags -开启一些插件 //使用后弹出这么些东西:“请小心,这些实验可能有风险”,不知会不会搞乱俺的配置啊!
chrome://extensions/ - 查看已经安装的扩展
其他的一些关于Chrome的实用参数及简要的中文说明(使用方法同上,当然也可以在shell中使用)
–user-data-dir=”[PATH]” 指定用户文件夹User Data路径,可以把书签这样的用户数据保存在系统分区以外的分区。
–disk-cache-dir=”[PATH]“ 指定缓存Cache路径
–disk-cache-size= 指定Cache大小,单位Byte
–first run 重置到初始状态,第一次运行
–incognito 隐身模式启动
–disable-javascript 禁用Javascript
--omnibox-popup-count="num" 将地址栏弹出的提示菜单数量改为num个。我都改为15个了。
--user-agent="xxxxxxxx" 修改HTTP请求头部的Agent字符串,可以通过about:version页面查看修改效果
--disable-plugins 禁止加载所有插件,可以增加速度。可以通过about:plugins页面查看效果
--disable-javascript 禁用JavaScript,如果觉得速度慢在加上这个
--disable-java 禁用java
--start-maximized 启动就最大化
--no-sandbox 取消沙盒模式
--single-process 单进程运行
--process-per-tab 每个标签使用单独进程
--process-per-site 每个站点使用单独进程
--in-process-plugins 插件不启用单独进程
--disable-popup-blocking 禁用弹出拦截
--disable-plugins 禁用插件
--disable-images 禁用图像
--incognito 启动进入隐身模式
--enable-udd-profiles 启用账户切换菜单
--proxy-pac-url 使用pac代理 [via 1/2]
--lang=zh-CN 设置语言为简体中文
--disk-cache-dir 自定义缓存目录
--disk-cache-size 自定义缓存最大值(单位byte)
--media-cache-size 自定义多媒体缓存最大值(单位byte)
--bookmark-menu 在工具 栏增加一个书签按钮
--enable-sync 启用书签同步
1. ChromeDriver加载插件
File file = new File ("files\\youtube.crx");
ChromeOptions options = new ChromeOptions();
options.addExtensions(file);
WebDriver driver = new ChromeDriver(options);
2. ChromeDriver切换浏览器语言
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=" + "zh-CN");
WebDriver driver = new ChromeDriver(options);
3. ChromeDriver设置启动chrome为默认用户的配置信息(包括书签、扩展程序、代理设置等)
// Windows下<br>ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");
WebDriver driver = new ChromeDriver(options);
4. ChromeDriver设置忽略 Chrome 浏览器证书错误报警提示
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type", "--ignore-certificate-errors"); WebDriver driver = new ChromeDriver(options);
//Or Set chrome browser's capabilities to to accept SSL certificate on runtime. DesiredCapabilities capability = DesiredCapabilities.chrome(); capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); WebDriver driver = new ChromeDriver(capability);
5. ChromeDriver设置Chrome参数使浏览器最大化
ChromeOptions options = new ChromeOptions();
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
options.addArguments("--test-type", "--start-maximized");<br>WebDriver driver = new ChromeDriver(options);
------------------------------国外官方资料---------------------------------------
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.
Using the ChromeOptions classYou can create an instance of ChromeOptions options = new ChromeOptions(); Alternatively, you can add the options to an already existing DesiredCapabilities capabilities = DesiredCapabilities.chrome(); // Add the WebDriver proxy capability. Using DesiredCapabilities directlyThe JavaMap<String, Object> chromeOptions = new Map<String, Object>(); Rubycaps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "--disable-web-security" ]}) Common use casesUse 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 ChromeOptions options = new ChromeOptions(); You can create your own custom profile by just running Chrome (on the command-line or through ChromeDriver) with the Start Chrome maximizedChromeOptions options = new ChromeOptions(); Using a Chrome executable in a non-standard locationChromeOptions options = new ChromeOptions(); Set a Chrome preferenceChromeOptions options = new ChromeOptions(); List of recognized capabilitiesThis is a list of all the WebDriver-standard capabilities that ChromeDriver supports: |
Name | Type | Default | Description |
| 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. |
| string | | 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') |
| list 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) |
| dictionary | | 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. |
| dictionary | | 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. |
| boolean | false | 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. |
| string | | An address of a Chrome debugger server to connect to, in the form of <hostname/ip:port>, e.g. '127.0.0.1:38947' |
| list of strings | | List of Chrome command line switches to exclude that ChromeDriver by default passes when starting Chrome. Do not prefix switches with --. |
| string | | Directory to store Chrome minidumps . (Supported only on Linux.) |
| dictionary | | A dictionary with either a value for “deviceName,” or values for “deviceMetrics” and “userAgent.” Refer to Mobile Emulation for more information. |
| dictionary | | 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. |