1. 程式人生 > >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 啟用書籤同步

  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);
  }