Selenium WebDriver / Firefox | Chrome / Java如何禁用图像加载

时间:2018-01-17 17:13:47

标签: java selenium firefox webdriver

为了测试(和带宽!)的目的,我需要禁用图像加载。

1 个答案:

答案 0 :(得分:5)

<强>火狐

FirefoxOptions options = new FirefoxOptions();
options.addPreference("permissions.default.image", 2);
WebDriver driver = new FirefoxDriver(options);

docs

中找到的线索
  

用于设置自定义首选项,我们建议使用prefs条目而不是传递个人资料。

DesiredCapabilities capabilities = new DesiredCapabilities();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("permissions.default.image", 2);
capabilities.setCapability("firefox_profile", profile);
WebDriver driver = new FirefoxDriver(capabilities);

<强>铬

HashMap<String, Object> images = new HashMap<String, Object>();
images.put("images", 2);
HashMap<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values", images);
ChromeOptions chrome_options =new ChromeOptions();
chrome_options.setExperimentalOption("prefs", prefs);
DesiredCapabilities chromeCaps = DesiredCapabilities.chrome();
chromeCaps.setCapability(ChromeOptions.CAPABILITY, chrome_options);
WebDriver driver = new ChromeDriver(chromeCaps);