selenium不同瀏覽器處理證書(SSL Certificate)的方法


在線英文版webdriverAPI 

https://www.w3.org/TR/webdriver/#proxy 

 

1.Firefox

  1. 創建一個新的profile,命名為certificateIssue 
    (創建方法參照:http://blog.csdn.net/qiyueqinglian/article/details/43053531)
  2. 用剛才創建的profile打開ffx瀏覽器
  3. 訪問報SSL certificate error的URL,通過點擊 I Understnad the Risks -> Add Exception button,引進證書,這樣就不報錯了.
  4. 在代碼里配置通過剛才創建的profile打開ffx 
    代碼如下
  5. ProfilesIni firProfiles = new ProfilesIni();
    FirefoxProfile wbdrverprofile = firProfiles.getProfile("certificateIssue");
    //是不是設置了下面兩行,其實就不用手動創建profile,並認證,然后手動引進證書。
    wbdrverprofile.setAcceptUntrustedCertificates(true); 
    wbdrverprofile.setAssumeUntrustedCertificateIssuer(false);
    WebDriver Driver = new FirefoxDriver(wbdrverprofile); 
    Driver.get("site URL where certificate error"); 

 firefox 新建firefoxDriver(profile)就報錯的解決辦法,根本原因是在selenium3.6中不支持此構造方法:

 

以下這個例子是親測通過的。

   File driverpath = new File("src/test/java/cn/com/jit/driver/geckodriver.exe"); 
    System.setProperty("webdriver.gecko.driver",driverpath.getAbsolutePath()); //第1、2行設置webdriver路徑
    ProfilesIni pi = new ProfilesIni(); 
    FirefoxProfile profile = pi.getProfile("default"); //第3、4行設置系統現在使用的默認 profile
    FirefoxOptions options = new FirefoxOptions().setProfile(profile); //使參數轉化為options,再new的時候當參數傳入
    WebDriver driver = new FirefoxDriver(options);

 

2.Chrome

//Set chrome browser's capabilities to to accept SSL certificate on runtime.
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");

WebDriver driver = new ChromeDriver(capability);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//Enter the URL of site where you facing SSL error.
driver.get("Enter site URL");

 

3.IE

IE需要通過js來解決。在你遇到證書錯誤的頁面點擊F12,就會看到繼續訪問該頁面的ID是”overridelink”。 
點擊這個就可以繼續訪問了。 
這里寫圖片描述

// Set path of IEDriverServer.exe
 System.setProperty("webdriver.ie.driver", "D://IEDriverServer.exe");
  WebDriver driver = new InternetExplorerDriver();
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  driver.get("URL of SSL error site");

  //To click on "Continue to this website (not recommended)." link to load original website.
  driver.navigate().to("javascript:document.getElementById('overridelink').click()");

 

Chrome Options參數收集---------------------------


-

 

 
0 --purge-memory-button 在Chrome的任務管理器中增加內存清理功能 


序號    參數    說明
1    --allow-outdated-plugins     不停用過期的插件。
2    --allow-running-insecure-content     默認情況下,https 頁面不允許從 http 鏈接引用 javascript/css/plug-ins。添加這一參數會放行這些內容。
3    --allow-scripting-gallery     允許拓展腳本在官方應用中心生效。默認情況下,出於安全因素考慮這些腳本都會被阻止。
4    --disable-accelerated-video     停用 GPU 加速視頻。
5    --disable-dart     停用 Dart。
6    --disable-desktop-notifications     禁用桌面通知,在 Windows 中桌面通知默認是啟用的。
7    --disable-extensions     禁用拓展。
8    --disable-file-system     停用 FileSystem API。
9    --disable-preconnect     停用 TCP/IP 預連接。
10    --disable-remote-fonts     關閉遠程字體支持。SVG 中字體不受此參數影響。
11    --disable-speech-input     停用語音輸入。
12    --disable-web-security     不遵守同源策略。
13    --disk-cache-dir     將緩存設置在給定的路徑。
14    --disk-cache-size     設置緩存大小上限,以字節為單位。
15    --dns-prefetch-disable     停用DNS預讀。
16    --enable-print-preview     啟用打印預覽。
17    --extensions-update-frequency     設定拓展自動更新頻率,以秒為單位。
18    --incognito     讓瀏覽器直接以隱身模式啟動。
19    --keep-alive-for-test     最后一個標簽關閉后仍保持瀏覽器進程。(某種意義上可以提高熱啟動速度,不過你最好得有充足的內存)
20    --kiosk     啟用kiosk模式。(一種類似於全屏的瀏覽模式)
21    --lang     使用指定的語言。
22    --no-displaying-insecure-content     默認情況下,https 頁面允許從 http 鏈接引用圖片/字體/框架。添加這一參數會阻止這些內容。
23    --no-first-run     跳過 Chromium 首次運行檢查。
24    --no-referrers     不發送 Http-Referer 頭。
25    --no-sandbox     徹底停用沙箱。
26    --no-startup-window     啟動時不建立窗口。
27    --proxy-pac-url     使用給定 URL 的 pac 代理腳本。(也可以使用本地文件,如 --proxy-pac-url="file:\\\c:\proxy.pac")
28    --proxy-server     使用給定的代理服務器,這個參數只對 http 和 https 有效。(例如 --proxy-server=127.0.0.1:8087 )
29    --single-process     以單進程模式運行 Chromium。(啟動時瀏覽器會給出不安全警告)
30    --start-maximized     啟動時最大化。
31    --user-agent     使用給定的 User-Agent 字符串

參數:--user-data-dir=UserDataDir
用途:自訂使用者帳戶資料夾(如:–user-data-dir="D:\temp\Chrome User Data")
參數:--process-per-tab
用途:每個分頁使用單獨進程
參數:--process-per-site
用途:每個站點使用單獨進程
參數:--in-process-plugins
用途:插件不啟用單獨進程

參數:--disable-popup-blocking
用途:禁用彈出攔截
參數:--disable-javascript
用途:禁用JavaScript
參數:--disable-java
用途:禁用Java
參數:--disable-plugins
用途:禁用插件
參數:–disable-images
用途:禁用圖像
參數:--omnibox-popup-count=”num”
用途:將網址列彈出的提示選單數量改為num個
參數:--enable-vertical-tabs
用途:調整chrome游覽器標簽存放在左邊,非頂部

 

如何使用這些參數

可以參考 Chromium 團隊的這個說明(英文) http://www.chromium.org/developers/how-tos/run-chromium-with-flags

對於 Windows 系統,簡單來說就是: 1. 在快捷方式上右鍵,選擇屬性。 1. 選擇“快捷方式”標簽。 1. 在“目標”一行的末尾,添加上啟動參數。 1. 最終效果應該像這樣: ……\chrome.exe --first-switch --second-switch

 

Chromium命令行啟動參數

說明

這個頁面列出了一些常用的命令行啟動參數以及對它們的簡要說明。說明中括號里的內容為譯者注。 為此頁+1

對於選項頁 ( chrome://settings/ ) 或實驗頁 ( chrome://flags/ ) 已明確給出可視選項的參數,本列表不再包含。

英文500個參數

https://peter.sh/experiments/chromium-command-line-switches/

https://cs.chromium.org/chromium/src/chrome/common/chrome_switches.cc


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM