![](/img/trans.png)
[英]Selenium WebDriver/Firefox|Chrome/Java How to disable image loading
[英]How to disable Chrome Plugins in Selenium WebDriver using Java
當我為此應用程序執行自動化代碼時,將顯示上面的彈出窗口。 現在我需要知道如何使用Java在Selenium WebDriver中禁用PDF Viewer插件。
這就是我現在正在使用的不起作用的地方。
DesiredCapabilities capabilities = DesiredCapabilities
.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments(new String[] { "test-type" });
options.addArguments(new String[] { "disable-extensions" });
String pluginToDisable = "Chrome PDF Viewer";
options.addArguments("plugins.plugins_disabled", pluginToDisable);
capabilities.setCapability("chrome.binary",
chromeDriver.getAbsolutePath());
capabilities.setCapability(ChromeOptions.CAPABILITY,
options);
options.addArguments("--lang=en-gb");
GlobalVars.driver = new ChromeDriver(capabilities);
已針對Chrome V更新:57
此解決方案不再有效。
這是C#中的有效實現:
var options = new ChromeOptions();
// This was a PAIN. If this ever does not work, here is how I got the preference name:
// 1. Navigate to : chrome://settings/content
// 2. Scroll to the bottom "PDF Documents" section
// 3. Right-Click and inspect element on the check box titled "Open PDF files in the default PDF viewer application"
// 4. The preference name is the pref key for the input, in this case: pref="plugins.always_open_pdf_externally"
options.AddUserProfilePreference("plugins.always_open_pdf_externally", true);
// The constructor must be pointed to the chrome driver .exe
// Or you must set a PATH variable pointing to the location
using (var driver = new ChromeDriver(options))
{
..........
以下是使用Selenium / Chrome禁用Flash和PDF查看器的示例:
ChromeOptions options = new ChromeOptions();
Map<String, Object> preferences = new Hashtable<String, Object>();
options.setExperimentalOption("prefs", preferences);
// disable flash and the PDF viewer
preferences.put("plugins.plugins_disabled", new String[] {
"Adobe Flash Player",
"Chrome PDF Viewer"
});
// launch the browser and navigate to the page
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://www.google.co.uk");
似乎“plugins.plugins_disabled”或“plugins.plugins_list”不再起作用了。 “plugins.always_open_pdf_externally”選項對我有用 ( mheirendt的解決方案)。 Java中的示例:
ChromeOptions co = new ChromeOptions();
Map<String,Object> prefs = new HashMap<>();
prefs.put("plugins.always_open_pdf_externally", true);
co.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(co);
---這對我有用 -
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("plugins.plugins_disabled", new String[] {"Adobe Flash Player", "Chrome PDF Viewer"});
chromePrefs.put("profile.default_content_settings.popups", 0);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
GlobalVars.driver = new ChromeDriver(cap);
由於Chrome的設置頁面也是一個Web應用程序,因此您可以通過編程方式抓取設置頁面並禁用PDF查看器。 這是一個示例python代碼:
browser = webdriver.Chrome("./chromedriver")
browser.get("chrome://settings-frame/content")
pdf_section = browser.find_element_by_id("pdf-section")
pdf_disable_checkbox = pdf_section.find_element_by_tag_name("input")
if not pdf_section.is_selected():
pdf_disable_checkbox.click()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.