当前位置: 首页 > 面试题库 >

在Selenium Google ChromeDriver中禁用图像

薛俊美
2023-03-14
问题内容

通过Selenium和c#使用图像时,如何禁用Google chrome中的图像?

我尝试了6种方法,但均无效果。

  • Chrome驱动程序:V2.2
  • Chrome版本:V29.0.1547.66 m
  • selenium:V2.35

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

尝试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=zh-
CN

)。该扩展名可防止首先下载图像。现在,只需使用以下语句加载它即可:

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


 类似资料:
  • 我想禁用箭头图像后,它被点击,并有选项使它再次启用后,另一个事件。 最后一行是我临时写的-onArrowClick被识别,但'enabled'不被识别

  • 问题内容: 我们的一个内部应用程序在页面上有一个Flash对象。当浏览器中 没有 可用的Flash插件/未安装Flash插件时,我需要测试以下 几种情况 : 内部(pepperflash)或系统范围的Adobe Flash Player插件 。 根据Chromium命令行开关列表文档页面,有一个相关的命令行开关: -禁用捆绑的PPAPI版本的Flash。 但是,这只会关闭Pepperflash内部

  • 我需要禁用我的服务器中的GC开销限制。这样做的原因是暂时防止异常。 但是我在Linux Java系统方面是零,对于如何运行这个命令行没有任何线索:

  • (在中)有一个名为的属性,它可以轻松地禁用/启用所述布局约束。 要为视图控制器创建第二个布局集,然后可以通过编程方式启用/禁用该布局集(通过两个布局集的NSLayoutConstraints的IBMoutletCollection),我需要在interface builder中禁用已定义的布局约束。 让我明确一下,我不想删除它们,只需禁用它们,这样我就可以设计第二个集合,而无需界面生成器一直抱怨约

  • 问题内容: 在Swing中,我们可以禁用这样的按钮: 无论如何,使用JavaFX Button可以做到这一点吗?用户只能按一次按钮。 问题答案: 当然。只有相关 属性具有相反的语义,并称为。这意味着您可以使用(not )和。由于它是JavaFX属性,因此您还可以将侦听器附加到。 在http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#

  • 问题内容: 我用Java开发的软件有问题。用户必须在JtextPane中编写文本。如果没有时间给他写信,则会出现一个弹出窗口。唯一的问题是:如果在弹出消息时用户正在写,则弹出窗口消失,因为按下的键验证了消息。 无论如何,我可以禁用此功能吗?我用它来创建MessageDialog: 谢谢阅读。 问题答案: 禁用正常的应用程序行为(例如,使用键操作对话框)不是一个好主意。它使许多用户感到困惑,这些用户