问题描述
如何在通过 Selenium 和 c# 使用 Google chrome 时禁用图像?
How does one disable images in Google chrome when using it through Selenium and c#?
我尝试了 6 种方法,但都没有奏效.我什至尝试过 this StackOverflow 问题的答案,但我认为信息在它已经过时了.
I've attempted 6 ways and none worked. I've even tried the answer on this StackOverflow question, however I think the info in it is out of date.
- Chrome 驱动程序:V2.2
- Chrome 版本:V29.0.1547.66 m
- 硒:V2.35
我所做的所有尝试都不会导致异常,它们运行正常但仍然显示图像:
All the attempts I've made don't cause exceptions, they run normally but still display images:
尝试 1:
ChromeOptions co = new ChromeOptions();
co.AddArgument("--disable-images");
IWebDriver driver = new ChromeDriver(co);
尝试 2:
DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability("chrome.switches", new string[1] { "disable-images" });
尝试 3:
ChromeOptions co = new ChromeOptions();
co.AddAdditionalCapability("chrome.switches", new string[1] { "disable-images" });
尝试 4:
var imageSetting = new Dictionary<string, object>();
imageSetting.Add("images", 2);
Dictionary<string, object> content = new Dictionary<string, object>();
content.Add("profile.default_content_settings", imageSetting);
var prefs = new Dictionary<string, object>();
prefs.Add("prefs", content);
var options = new ChromeOptions();
var field = options.GetType().GetField("additionalCapabilities", BindingFlags.Instance | BindingFlags.NonPublic);
if (field != null)
{
var dict = field.GetValue(options) as IDictionary<string, object>;
if (dict != null)
dict.Add(ChromeOptions.Capability, prefs);
}
尝试 5:
ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("profile.default_content_settings", 2);
尝试 6:
Dictionary<String, Object> contentSettings = new Dictionary<String, Object>();
contentSettings.Add("images", 2);
Dictionary<String, Object> preferences = new Dictionary<String, Object>();
preferences.Add("profile.default_content_settings", contentSettings);
DesiredCapabilities caps = DesiredCapabilities.Chrome();
caps.SetCapability("chrome.prefs", preferences);
推荐答案
使用http://chrome-extension-downloader.com/ 下载阻止图像"扩展程序 (https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj?hl=en-GB).该扩展程序首先防止图像被下载.现在只需使用以下语句加载它:
Use http://chrome-extension-downloader.com/ to download the "Block Image" extension (https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj?hl=en-GB). The extension prevents the image from being downloaded in the first place. Now it's just a matter of loading it using the following statement:
var options = new ChromeOptions();
//use the block image extension to prevent images from downloading.
options.AddExtension("Block-image_v1.0.crx");
var driver = new ChromeDriver(options);
这篇关于在 Selenium Google ChromeDriver 中禁用图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!