【问题标题】:How to disable Chrome Plugins in Selenium WebDriver using Java如何使用 Java 在 Selenium WebDriver 中禁用 Chrome 插件
【发布时间】:2016-06-03 14:30:51
【问题描述】:

Chrome Plugin pop up

当我为此应用程序执行我的自动化代码时,会显示上述弹出窗口。现在我需要知道如何使用 Java 在 Selenium WebDriver 中禁用 PDF 查看器插件。

这是我现在使用的,但不起作用。

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

【问题讨论】:

  • 我也尝试过不同的组合。还是不行。选项#1:options.addArguments("disable-plugins"); GlobalVars.driver = new ChromeDriver(options);选项 #2: Map prefs = new HashMap(); prefs.put("plugins.plugins_disabled", "Chrome PDF Viewer"); options.setExperimentalOption("prefs", prefs); GlobalVars.driver = new ChromeDriver(options);

标签: java google-chrome selenium plugins


【解决方案1】:

为 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))
        {
        ..........

【讨论】:

    【解决方案2】:

    这是一个使用 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");
    

    【讨论】:

    • 如果我想在驱动运行时禁用插件怎么办?
    【解决方案3】:

    似乎“plugins.plugins_disabled”或“plugins.plugins_list”不再起作用了。选项“plugins.always_open_pdf_externally”对我有用(mheirendt's 解决方案)。 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);
    

    【讨论】:

      【解决方案4】:

      ---这对我有用--

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

      【讨论】:

        【解决方案5】:

        由于 Chrome 的设置页面也是一个网络应用程序,您可以通过编程方式抓取设置页面并禁用 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()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-13
          • 2021-05-15
          • 1970-01-01
          • 1970-01-01
          • 2011-12-25
          • 1970-01-01
          相关资源
          最近更新 更多