禁用图像硒谷歌ChromeDriver [英] Disable images in Selenium Google ChromeDriver

查看:509
本文介绍了禁用图像硒谷歌ChromeDriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过硒和C#中使用时,它如何在谷歌浏览器做一件禁用图像?



我已经尝试6种方法,没有工作。我甚至试过在这个的StackOverflow的问题,但是我觉得在信息它是过时




  • Chrome的驱动程序:V2.2

  • Chrome版本:V29。 0.1547.66米

  • 硒:V2.35



所有的尝试我做了不会引起异常,它们正常运行,但仍显示图像:



1的尝试:

  ChromeOptions共同=新ChromeOptions(); 
co.AddArgument( - 禁用的图像);
IWebDriver司机=新ChromeDriver(CO);



尝试2:

  DesiredCapabilities能力= DesiredCapabilities.Chrome(); 
capabilities.SetCapability(chrome.switches,新的字符串[1] {禁用的图像});



尝试3:

  ChromeOptions共同=新ChromeOptions(); 
co.AddAdditionalCapability(chrome.switches,新的字符串[1] {禁用的图像});



尝试4:

  VAR照排=新词典<字符串对象>(); 
imageSetting.Add(图像,2);
&字典LT;字符串对象>内容=新词典<字符串对象>();
content.Add(profile.default_content_settings,照排);
变种首选项=新词典<字符串对象>();
prefs.Add(首选项,内容);
变种选项=新ChromeOptions();
VAR字段= options.GetType()getfield命令(additionalCapabilities,BindingFlags.Instance | BindingFlags.NonPublic可);
如果(场!= NULL)
{
VAR字典= field.GetValue(期权)作为IDictionary的<字符串对象> ;;
如果(字典!= NULL)
dict.Add(ChromeOptions.Capability,首选项);
}



尝试5:

  ChromeOptions选项=新ChromeOptions(); 
options.AddAdditionalCapability(profile.default_content_settings,2);



尝试6:

 词典<弦乐,对象> contentSettings =新词典<弦乐,对象>(); 
contentSettings.Add(图像,2);
&字典LT;弦乐,对象>喜好=新词典<弦乐,对象>();
preferences.Add(profile.default_content_settings,contentSettings);
DesiredCapabilities帽= DesiredCapabilities.Chrome();
caps.SetCapability(chrome.prefs,喜好);


解决方案

使用的http://chrome-extension-downloader.com/ 下载的图像块扩展名(的https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj?hl=en-GB )。扩展功能可以防止图像在第一时间下载。现在,它只是一个使用下面的语句加载它的事:

  VAR选项=新ChromeOptions(); 

//使用块图像扩展以避免照片下载。
options.AddExtension(块image_v1.0.crx);

变种司机=新ChromeDriver(选件);


How does one disable images in Google chrome when using it through Selenium and c#?

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 driver: V2.2
  • Chrome version: V29.0.1547.66 m
  • Selenium: V2.35

All the attempts I've made don't cause exceptions, they run normally but still display images:

Attempt 1:

ChromeOptions co = new ChromeOptions();
co.AddArgument("--disable-images");
IWebDriver driver = new ChromeDriver(co);

Attempt 2:

DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability("chrome.switches", new string[1] { "disable-images" });

Attempt 3:

ChromeOptions co = new ChromeOptions();
co.AddAdditionalCapability("chrome.switches", new string[1] { "disable-images" });

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

Attempt 5:

ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("profile.default_content_settings", 2);

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

解决方案

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

这篇关于禁用图像硒谷歌ChromeDriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆