2013-03-01 148 views
10

我一直在尋找這一整天,似乎沒有解決方案目前從python的chromedriver實現可用。設置chrome.prefs與python綁定硒在chromedriver

如何使用webdriver.Chrome()方法設置特定的chrome.prefs(例如profile.managed_default_content_settings.images = 2等配置文件設置)?

我已經通過webdriver.ChromeOptions()試過了,但沒有成功。在Java中,有適當的功能可以實現這一點。

但是Python?這是我在做什麼目前...

options = webdriver.ChromeOptions() 
    options.add_argument('--allow-running-insecure-content') 
    options.add_argument('--disable-web-security') 
    options.add_argument('--disk-cache-dir=/var/www/cake2.2.4/app/tmp/cache/selenium-chrome-cache') 
    options.add_argument('--no-referrers') 
    options.add_argument('--window-size=1003,719') 
    options.add_argument('--proxy-server=localhost:8118') 
    options.add_argument("'chrome.prefs': {'profile.managed_default_content_settings.images': 2}") 


    self.selenium = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver',chrome_options=options) 

回答

3

修復:

有通過避免chromeoptions對象,並回復到desiredcapabilities字典的解決方案(不推薦)。出於某種原因,selenium庫中的webdriver.py將一個空的chromeoptions字典添加到desiredcapabilities字典中,使其無用。所以,你需要取消註釋行54 webdriver.py

desired_capabilities.update(options.to_capabilities()) 

然後使用此代碼通過所有所需的功能,以chromedriver

CHROME = { 
"browserName": "chrome", 
     "version": "", 
     "platform": "ANY", 
     "javascriptEnabled": True, 
     "chrome.prefs": {"profile.managed_default_content_settings.images": 2}, 
     "proxy": { 
      "httpProxy":"localhost:8118", 
      "ftpProxy":None, 
      "sslProxy":None, 
      "noProxy":None, 
      "proxyType":"MANUAL", 
      "class":"org.openqa.selenium.Proxy", 
      "autodetect":False 
      }, 
     "chrome.switches": ["window-size=1003,719", "allow-running-insecure-content", "disable-web-security", "disk-cache-dir=/var/www/cake2.2.4/app/tmp/cache/selenium-chrome-cache", "no-referrers"], 
     } 


    self.selenium = webdriver.Chrome(desired_capabilities=CHROME) 
+0

類似的問題在這裏(我試圖改變Chrome的下載文件夾)。嘗試了你的代碼,但不知何故,它不適合我。我的webdriver.py文件中的desired_capabilities.update(options.to_capabilities())行沒有被註釋掉。有什麼想法嗎?你有沒有遇到過其他解決方案? – Parzival 2013-08-03 00:22:20

+0

不,這只是。你覺得這行嗎? desired_capabilities.update(options.to_capabilities()) – Jabb 2013-08-26 14:19:12

+0

我做過。它沒有被註釋掉,所以它應該有效,但不知道它沒有。最後,我完全放棄了Chrome。 – Parzival 2013-08-26 15:33:53

3

只是一個小更新別人絆倒這個問題。

對於較新版本的下面的代碼工作沒有問題:

options.add_experimental_option('prefs', {'download.default_directory':'C:\\temp'}) 
4

對於誰想要禁用chromedriver圖像的人,下面的代碼可能會幫助你。

from selenium.webdriver.chrome.options import Options 
chrome_options = Options() 
chrome_options.add_experimental_option("prefs", {'profile.default_content_settings.images': 2}) 
driver = webdriver.Chrome(chrome_options=chrome_options) 
4

這與至少2.15最新版本chromedriver到當前版本2.20是什麼在起作用:

chrome_options = Options() 
chrome_options.add_experimental_option("prefs", {'profile.managed_default_content_settings.images': 2}) 
chrome = webdriver.Chrome('/path/to/chromedriver',chrome_options=chrome_options) 
chrome.get("https://google.com") 
1

對於任何與Python語法掙扎,這裏有一個完整,工作示例。它禁用了Chrome的「您希望Google Chrome爲您的網站保存密碼嗎?」提示。另見WebDriver Chrome Browser: Avoid 'Do you want chrome to save your password' pop up

from selenium import webdriver 
from selenium.webdriver.chrome.options import Options 
chrome_options = Options() 
chrome_options.add_experimental_option('prefs', { 
    'credentials_enable_service': False, 
    'profile': { 
     'password_manager_enabled': False 
    } 
}) 
driver = webdriver.Chrome(chrome_options=chrome_options) 
driver.get('https://google.com')