selenium操作chrome时的一些配置

selenium操作chrome时的一些配置

收集的一些selenium chrome配置信息:
一些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 --allow-outdated-plugins 不停用过期的插件。
2 --allow-running-insecure-content 默认情况下,https 页面不允许从 http 链接引用 javascript/css/plug-ins。添加这一参数会放行这些内容。
3 --allow-scripting-gallery 允许拓展脚本在官方应用中心生效。默认情况下,出于安全因素考虑这些脚本都会被阻止。
4 --disable-accelerated-2d-canvas 停用 GPU 加速二维画布。
5 --disable-accelerated-video 停用 GPU 加速视频。
6 --disable-dart 停用 Dart。
7 --disable-desktop-notifications 禁用桌面通知,在 Windows 中桌面通知默认是启用的。
8 --disable-extensions 禁用拓展。
9 --disable-file-system 停用 FileSystem API。(注意一些拓展如 Adblock Plus for Google Chrome™ 依赖此 API 运行)
10 --disable-java 停用 Java。
11 --disable-local-storage 禁用 LocalStorage。
12 --disable-preconnect 停用 TCP/IP 预连接。
13 --disable-remote-fonts 关闭远程字体支持。SVG 中字体不受此参数影响。
14 --disable-speech-input 停用语音输入。
15 --disable-sync 停用同步功能。
16 --disable-ssl3 停用 SSL v3。
17 --disable-web-security 不强制遵守同源策略,供网站开发人员测试站点使用。
18 --disk-cache-dir 将缓存设置在给定的路径。
19 --disk-cache-size 设置缓存大小上限,以字节为单位。
20 --dns-prefetch-disable 停用DNS预读。
21 --enable-print-preview 启用打印预览。
22 --extensions-update-frequency 设定拓展自动更新频率,以秒为单位。
23 --incognito 让浏览器直接以隐身模式启动。
24 --keep-alive-for-test 最后一个标签关闭后仍保持浏览器进程。(某种意义上可以提高热启动速度,不过你最好得有充足的内存)
25 --kiosk 启用kiosk模式。(一种类似于全屏的浏览模式)
26 --lang 使用指定的语言。
27 --no-displaying-insecure-content 默认情况下,https 页面允许从 http 链接引用图片/字体/框架。添加这一参数会阻止这些内容。
28 --no-first-run 跳过 Chromium 首次运行检查。
29 --no-referrers 不发送 Http-Referer 头。
30 --no-sandbox 彻底停用沙箱。
31 --no-startup-window 启动时不建立窗口。
32 --proxy-pac-url 使用给定 URL 的 pac 代理脚本。(也可以使用本地文件,如 --proxy-pac-url="file:\c:\proxy.pac")
33 --proxy-server 使用给定的代理服务器,这个参数只对 http 和 https 有效。(例如 --proxy-server=127.0.0.1:8087 )
34 --show-component-extension-options 让自带的拓展组件显示在 chrome://settings/extensions 里。(目前有一个 "Bookmark Manager 0.1")
35 --single-process 以单进程模式运行 Chromium。(启动时浏览器会给出不安全警告)
36 --skip-gpu-data-loading 跳过启动时的 GPU 信息收集、黑名单读取与黑名单自动更新,这样一来,所有的 GPU 功能都可供使用,并且 about:gpu 页面会显示空白。此参数仅供测试使用。
37 --start-maximized 启动时最大化。
38 --touch-optimized-ui 使用对触屏更友好的用户界面。(目前来看似乎只是把一些字体放大了)
39 --user-agent 使用给定的 User-Agent 字符串。
  
  private void getChromeDriver() {
    Config config = new ConfigProvider().get();
    
    Config configToUse = Optional.ofNullable(config).orElse(ConfigLoader.load());
    prepareChromeDriver(configToUse);


    final String osName = config.getString("os.name");
    ChromeOptions chromeOptions = new ChromeOptions();
    if (StringUtils.containsIgnoreCase(osName, "windows")) { // 本地Windows环境, 初始化local环境的chrome driver
      logger.info("windows环境", osName);
      chromeOptions.setBinary(configToUse.getString("webdriver.chrome.binary")); // windows下chrome地址
      chromeOptions.addArguments("--profile-directory=target");
      chromeOptions.addArguments("--user-data-dir=target\\");
    } else { // FAT/UAT/PROD等Linux环境, 初始化Linux环境的chrome driver
      logger.info("Linux环境", osName);
      chromeOptions.setBinary("/usr/bin/google-chrome-stable"); // 配置chrome安装地址
      chromeOptions.addArguments("--headless");
      chromeOptions.addArguments("--disable-gpu");
      String proxyServer = qconfigService.getConfig("proxyHost") + ":" + qconfigService.getConfig("proxyPort"); //代理配置
      chromeOptions.addArguments("--proxy-server=" + proxyServer);
    }
    chromeOptions.addArguments("--incognito"); // 打开隐私模式
    chromeOptions.addArguments("--lang=" + chineses); // 中文
    chromeOptions.addArguments("-test-type", "--ignore-certificate-errors"); // 忽略证书错误
    chromeOptions.addArguments("--no-sandbox");


//    添加chrome模拟器
//    Map<String, String> mobileEmulation = new HashMap<String, String>();
//    mobileEmulation.put("deviceName", "Galaxy S5");//"Nexus 5X");
//    chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
    
    String webDriverPath = System.getProperty("webdriver.chrome.driver");
    ChromeDriverService driverService = new ChromeDriverService.Builder().usingDriverExecutable(new File(webDriverPath)).usingAnyFreePort().build();
    try {
      driverService.start();
    } catch (IOException e) {
      logger.error(e);
    }
    logger.info("webdriver.chrome.driver", webDriverPath);
    ChromeDriver driver = new ChromeDriver(driverService, chromeOptions);
    logger.info("webdriver.chrome.driver", webDriverPath);
  }

posted @ 2021-10-15 18:02  eedc  阅读(717)  评论(0编辑  收藏  举报