通过查看ChromeOptions类的源码,我们可以看到ChromeOptions类有下面这些方法。
用于配合Capabilities类使用,会在既需要设置启动浏览器的参数,又需要设置启动浏览器的版本号、平台参数时用到。具体可以参考我博客文章:https://blog.csdn.net/qq_37688023/article/details/105891378
public ChromeOptions merge(Capabilities extraCapabilities) {
super.merge(extraCapabilities);
return this;
}
对于一台机器上安装了多个版本的Chrome浏览器,可以使用setBinary 指定待测试Chrome。
public ChromeOptions setBinary(File path) {
this.binary = ((File)Preconditions.checkNotNull(path)).getPath();
return this;
}
public ChromeOptions setBinary(String path) {
this.binary = (String)Preconditions.checkNotNull(path);
return this;
}
如果希望Chrome 浏览器启动时附带启动参数,可通过addArguments 方式加载。
public ChromeOptions addArguments(String... arguments) {
this.addArguments((List)ImmutableList.copyOf(arguments));
return this;
}
public ChromeOptions addArguments(List arguments) {
this.args.addAll(arguments);
return this;
}
希望测试某个浏览器插件,可通过addExtensions方式提前加载以.crx 为扩展名的插件
public ChromeOptions addExtensions(File... paths) {
this.addExtensions((List)ImmutableList.copyOf(paths));
return this;
}
public ChromeOptions addExtensions(List paths) {
Iterator var2 = paths.iterator();
while(var2.hasNext()) {
File path = (File)var2.next();
Preconditions.checkNotNull(path);
Preconditions.checkArgument(path.exists(), "%s does not exist", path.getAbsolutePath());
Preconditions.checkArgument(!path.isDirectory(), "%s is a directory", path.getAbsolutePath());
}
this.extensionFiles.addAll(paths);
return this;
}
类似于addExtensions。
public ChromeOptions addEncodedExtensions(String... encoded) {
this.addEncodedExtensions((List)ImmutableList.copyOf(encoded));
return this;
}
public ChromeOptions addEncodedExtensions(List encoded) {
Iterator var2 = encoded.iterator();
while(var2.hasNext()) {
String extension = (String)var2.next();
Preconditions.checkNotNull(extension);
}
this.extensions.addAll(encoded);
return this;
}
感觉上和addArguments有点类似,不过setExperimentalOption是设置实验选项(ChromeDriver选项尚未通过ChromeOptions API公开),这个方法我比较经常用来设置浏览器默认下载路径。
public ChromeOptions setExperimentalOption(String name, Object value) {
this.experimentalOptions.put(Preconditions.checkNotNull(name), value);
return this;
}
public ChromeOptions setPageLoadStrategy(PageLoadStrategy strategy) {
this.setCapability("pageLoadStrategy", strategy);
return this;
}
public ChromeOptions setUnhandledPromptBehaviour(UnexpectedAlertBehaviour behaviour) {
this.setCapability("unhandledPromptBehavior", behaviour);
this.setCapability("unexpectedAlertBehaviour", behaviour);
return this;
}
public ChromeOptions setAcceptInsecureCerts(boolean acceptInsecureCerts) {
this.setCapability("acceptInsecureCerts", acceptInsecureCerts);
return this;
}
设置无头模式,后台运行,不弹出浏览器。
public ChromeOptions setHeadless(boolean headless) {
this.args.remove("--headless");
if (headless) {
this.args.add("--headless");
this.args.add("--disable-gpu");
}
return this;
}
public ChromeOptions setProxy(Proxy proxy) {
this.setCapability("proxy", proxy);
return this;
}
在上面这些方法中,addArguments是使用最多的一个方法,该方法后面参数的含义对应,可以参考下面改链接(打开可能有点慢)。
https://peter.sh/experiments/chromium-command-line-switches/
需要注意的是,addArguments方法中参数设置错了后,代码运行不会出错,且没有错误提示,只是对应的设置不会生效。
//创建ChromeOptions对象
ChromeOptions chromeOptions = new ChromeOptions();
//创建HashMap对象和指定下载路径
HashMap chromePrefs = new HashMap<>();
String downloadPath = new File("").getCanonicalPath()+"\\src\\test\\resources\\download";
//设置默认下载路径
chromePrefs.put("download.default_directory", downloadPath);
chromeOptions.setExperimentalOption("prefs",chromePrefs);
//创建WebDriver对象
WebDriver driver=new ChromeDriver(chromeOptions);
ChromeOptions chromeOptions=new ChromeOptions();
//方法一:通过setHeadless方法直接设置
chromeOptions.setHeadless(true);
//方法二:通过addArguments方法添加参数设置
chromeOptions.addArguments("--headless");
ChromeOptions chromeOptions=new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
ChromeOptions chromeOptions=new ChromeOptions();
chromeOptions.addArguments("--lang=zh-CN");
ChromeOptions chromeOptions=new ChromeOptions();
chromeOptions.addArguments("--disable-gpu");
参考链接:
https://blog.csdn.net/bpz31456/article/details/80455708
https://blog.csdn.net/zwq912318834/article/details/78933910