有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用Java将数据设置为Selenium Chrome驱动程序的ChromeOptions?

我尝试注入ChromeOptions标志autoplay-policy-Document user activation is required用于块自动播放视频以测试性能

我有几次尝试。但没有人不工作

ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = Maps.newHashMap();
prefs.put("--autoplay-policy", "Document user activation is required.");
options.setExperimentalOption("prefs", prefs);

或者

options.setCapability("--autoplay-policy", "Document user activation is required.");

我正在尝试使用pup up菜单使用URL chrome://flags/#autoplay-policy来设置Document user activation。但是当Selenium点击RELAUNCH NOW以应用我的测试崩溃时。我需要在启动Chrome之前注射

enter image description here

我正在寻找在chromedriver启动时阻止自动播放视频的任何机会。请帮我解决这个问题。谢谢


共 (1) 个答案

  1. # 1 楼答案

    我也有同样的问题,无法通过本机选项“自动播放策略”阻止视频的自动播放。与此同时,它不起作用,我用Chrome插件“禁用HTML5自动播放”启动Chrome。我附上了Selenium API中管理此插件的代码:

    public class PluginHTML5Autoplay extends PluginChrome {
    
        private final String fileName = "html5autoplay_0_6_2.crx";
        private static final String PageSetup = "chrome-extension://efdhoaajjjgckpbkoglidkeendpkolai/options.html";
        private static final String XPathSelect = "//select[@id='default-mode']";
    
        @Override
        public void addPluginToChrome(ChromeOptions options) throws Exception {
            super.addPluginToChrome(options, this.fileName);
        }
    
        public static void disableAutoplay(WebDriver driver) {
            driver.get(PageSetup);
            new Select(driver.findElement(By.xpath(XPathSelect))).selectByVisibleText("Disable autoplay");
        }
    
        public void disableNothing(WebDriver driver) {
            driver.get(PageSetup);
            new Select(driver.findElement(By.xpath(XPathSelect))).selectByVisibleText("Disable nothing");
        }
    }
    

    使用此插件启动Chrome时的默认选项是“禁用自动播放”。因此,你只需要管理插件,以防你需要激活自动播放