如何在Selenium中使用无头镀铬

时间:2018-05-01 19:39:02

标签: java flash selenium-chromedriver google-chrome-headless

作为CI流程的一部分,我尝试自动与我们的Flash应用进行一些互动。在使用Selenium Standalone Server无头地(通过xvfb-run)运行chrome时,我遇到了启用闪存的问题。我已经做了很多搜索,但到目前为止还没有找到任何有用的东西。

我目前正在使用此功能,但如果有某个已知的工作配置,我可以切换到不同的版本...

  • Selenium Standalone Server 3.11
  • Chromedriver 2.33
  • Chrome 65.0.3325.181
  • Java 8

当我第一次启动时,我会在页面上收到警告,说我需要启用Adobe Flash Player。我得到了#34;过去"该消息使用https://sqa.stackexchange.com/questions/30312/enable-flash-player-on-chrome-62-while-running-selenium-test中的以下内容:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("headless");

    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("profile.default_content_setting_values.plugins", 1);
    prefs.put("profile.content_settings.plugin_whitelist.adobe-flash-player", 1);
    prefs.put("profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player", 1);
    // Enable Flash for this site
    prefs.put("PluginsAllowedForUrls", "ourapp.com");
    options.setExperimentalOption("prefs", prefs);

    WebDriver driver = new ChromeDriver(options);
    driver.get("ourapp.com");

加载我们的应用时,该页面现在提供了一个稍微不同的消息,我无法通过该消息。有没有办法解决这个问题,或者是否有其他方法可以默认启用Flash?

Restart Chrome to enable Adobe Flash Player

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

感谢同事指出这篇文章,表明插件不能用无头镀铬工作。 https://groups.google.com/a/chromium.org/forum/#!searchin/headless-dev/flash%7Csort:date/headless-dev/mC0REfsA7vo/rKAZdRrCCQAJ

幸运的是,在我的情况下,我已经使用xvfb作为虚拟显示器,所以删除了#34;无头&#34;来自我的ChromeOptions的论据就是让一切运行所需的全部。

修改*

虽然我在使用xvfb时不需要在无头模式下运行,但我最终发现了真正的解决方案&#39; (更多的解决方法)我的问题是将自定义chrome配置文件上传到我的docker镜像。这样做允许我设置我需要的所有首选项,并且不需要在原始帖子中发布的代码。更愿意以编程方式实现这一目标,但这至少可以让我获得我现在所需要的东西。如果其他人在将来遇到这种情况,我会发布帖子。