selenium啓動Chrome的進階配置參數

Posted in :

建立了Chrome Options 物件之後就是新增引數,新增引數有幾個特定的方法,分別對應新增不同型別的配置專案,常見的方法:

from selenium import webdriver
option = webdriver.ChromeOptions()

#增加啓動chrome參數
option.add_argument()

#增加擴展應用
option.add_extension()
option.add_encoded_extension()

#增加實驗性質的設置參數
option.add_experimental_option()

#增加除錯器
option.debugger_address()

常用配置參數:

from selenium import webdriver
 option = webdriver.ChromeOptions()

#增加UserAgent
options.add_argument('user-agent="MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"')

#指定瀏覽器解析度
options.add_argument('window-size=1920x3000') 

#加上這個屬性來規避bug
chrome_options.add_argument('--disable-gpu') 

#隱藏scroll bar, 應對一些特殊頁面
options.add_argument('--hide-scrollbars')

#不加載圖片, 提升速度
options.add_argument('blink-settings=imagesEnabled=false') 

#瀏覽器不提供可視化頁面. linux下如果系統不支持可視化不加這條會啓動失敗
options.add_argument('--headless') 

#以最高權限運行
options.add_argument('--no-sandbox')

#手動指定使用的瀏覽器位置
options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" 

#增加crx插件
option.add_extension('d:\crx\AdBlock_v2.17.crx') 

#禁用JavaScript
option.add_argument("--disable-javascript") 

#設置開發者模式啓動,該模式下webdriver屬性為正常值
options.add_experimental_option('excludeSwitches', ['enable-automation']) 

#禁用瀏覽器彈窗
 prefs = {
     'profile.default_content_setting_values' :  {  
         'notifications' : 2  
      }  
 }  
options.add_experimental_option('prefs',prefs)

#禁用「Save password」彈窗
 prefs = {
      "profile.password_manager_enabled": False, "credentials_enable_service": False
  }  
 options.add_experimental_option('prefs',prefs)

driver=webdriver.Chrome(chrome_options=chrome_options)

其他參數:

--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-TW
 設定語言為繁體中文

 --disk-cache-dir 
 自定義快取目錄

 --disk-cache-size 
 自定義快取最大值(單位byte)

 --media-cache-size 
 自定義多媒體快取最大值(單位byte)

 --bookmark-menu 
 在工具 欄增加一個書籤按鈕

 --enable-sync 
 啟用書籤同步

相關文章:

Capabilities & ChromeOptions
https://sites.google.com/a/chromium.org/chromedriver/capabilities

完整參數:
https://peter.sh/experiments/chromium-command-line-switches/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *