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

Amr*_*tfy 0 java firefox selenium webdriver

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

Amr*_*tfy 6

火狐

FirefoxOptions options = new FirefoxOptions();
options.addPreference("permissions.default.image", 2);
WebDriver driver = new FirefoxDriver(options);
Run Code Online (Sandbox Code Playgroud)

文档中找到的线索

为了设置自定义首选项,我们建议使用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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)