首页 > 其他分享 >selenium chrome浏览器启动设置,加载浏览器相关插件设置方法。

selenium chrome浏览器启动设置,加载浏览器相关插件设置方法。

时间:2022-12-18 23:31:19浏览次数:57  
标签:插件 浏览器 Chrome -- ChromeDriver ChromeOptions 设置 new options

 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. 


  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

See ​​https://code.google.com/p/selenium/wiki/DesiredCapabilities#JSON_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.


​perfLoggingPrefs​​ object

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







 Name

Type

Default 

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.

​binary​

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')

​extensions​

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)

​localState​

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.

​prefs​

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.

​detach​

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.

​debuggerAddress​

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'

​excludeSwitches​

list 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.)

​mobileEmulation​

dictionary

 

A dictionary with either a value for “deviceName,” or values for “deviceMetrics” and “userAgent.” Refer to Mobile Emulation for more information.

​perfLoggingPrefs​

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.

 



标签:插件,浏览器,Chrome,--,ChromeDriver,ChromeOptions,设置,new,options
From: https://blog.51cto.com/u_14181351/5951250

相关文章

  • 使用浏览器inspect调试app
    使用浏览器inspect调试app在开发混合项目的过程中,常常需要在app环境排查问题,接口可以使用fiddler等工具来抓包,但是js错误就不好抓包了,这里介绍一种调试工具-浏览器。1.调......
  • element的v-loading有了设置的全局默认loading再次设置loading
     这个就是展示loading默认类名直接隐藏 /deep/ .el-loading-spinner{ display:none!important;}我们再次设置loading就行了/deep/.el-loading-parent-......
  • 如何实现chrome谷歌浏览器多开(独立环境 独立cookie)
    由于各种各样的原因,你可能需要在一个电脑登录某个平台,比如一个电脑登录3个公众号,或者3个知乎等等。最简单的方案是,直接安装3个不同的浏览器,比如一个谷歌浏览器,一个火狐浏......
  • Web Deploy发布网站错误 检查授权和委派设置
    webDeploy发布asp.net网站给我们提供方便,开始配置好了可以方便的发布网站,但是没过多久就出现无法执行此操作。请与服务器管理员联系,检查授权和委派设置。花了好长时间找到......
  • CitectVBA定制过程分析器7:设置时间轴跨度
    这是我在新浪博客写的一篇学习笔记,在这里也发表一次。CitectVBA定制过程分析器7:设置时间轴跨度_来自金沙江的小鱼_新浪博客(sina.com.cn)前面的练习中尝试过通过CitectV......
  • 浏览器渲染原理
    看渲染过程先了解几个概念浏览器进程进程:程序的一次执行,它占有一片独有的内存空间,是操作系统执行的基本单元;线程:是进程内的一个独立执行单元,是CPU调度的最小单元;C......
  • Visual Studio的调试选项卡的工作目录和环境设置
    https://blog.csdn.net/a_Treasure/article/details/83867465vs的“属性页”设置让初学者很是头疼,记录一下“配置属性”的“常规”和“调试”选项卡的设置方法。1.常规......
  • 超级好看的 Edge 浏览器新标签页插件:好用、好看、免费浏览器必备
    BdTab新标签页BdTab新标签页扩展是一款免费无广告、简单好用的的高颜值新标签页扩展。BdTab它颜值高、简单好用、支持高度自定义:在登录之后支持云备份,支持快速切换搜索引擎......
  • 超级好看的 Edge 浏览器新标签页插件:好用、好看、免费浏览器必备
    BdTab新标签页BdTab新标签页扩展是一款免费无广告、简单好用的的高颜值新标签页扩展。BdTab它颜值高、简单好用、支持高度自定义:在登录之后支持云备份,支持快速切换搜......
  • win11桌面改成win7桌面的设置方法
    win11桌面改成win7桌面的设置方法win11系统是新推出的操作系统,界面风格较大幅度的更改,用户升级之后并很喜欢,想换成win7桌面,下面就来看看具体的操作吧。操作方法:1、首先需要......