Java 使用实验功能(download.default_目录)和selenide浏览器默认设置为selenide设置ChromeDriver
我正在寻找最好的方法来设置chromium浏览器,以便在接受默认selenide设置的同时允许下载 我们让ChromeDriver完全基于selenium系统属性(无头、窗口大小等)工作 我现在可以下载文件了,但它是通过硬编码属性进入chrome驱动程序的,这些属性也是在系统配置文件中定义的 因此,我想知道如何允许将文件下载到指定的文件夹中(在创建ChromeDriver时动态下载),同时仍使用selenide配置中的其他默认设置 因此,我的问题是:Java 使用实验功能(download.default_目录)和selenide浏览器默认设置为selenide设置ChromeDriver,java,selenium,selenium-webdriver,selenium-chromedriver,selenide,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,Selenide,我正在寻找最好的方法来设置chromium浏览器,以便在接受默认selenide设置的同时允许下载 我们让ChromeDriver完全基于selenium系统属性(无头、窗口大小等)工作 我现在可以下载文件了,但它是通过硬编码属性进入chrome驱动程序的,这些属性也是在系统配置文件中定义的 因此,我想知道如何允许将文件下载到指定的文件夹中(在创建ChromeDriver时动态下载),同时仍使用selenide配置中的其他默认设置 因此,我的问题是: 将selenide的系统定义属性与在Chro
private void saveDownloadsIntoDesiredDirectory(String downloadFolderName) {
if (Configuration.browser.equals("chrome")) {
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
//THIS must be the canonicalPath,otherwise it won't work on Windows.
try {
chromePrefs.put("download.default_directory", new File(downloadFolderName).getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
chromePrefs.put("download.prompt_for_download", false);
chromePrefs.put("download.directory_upgrade", true);
chromePrefs.put("safebrowsing.enabled", true);
//THIS took forever to find.
chromePrefs.put("safebrowsing.disable_download_protection",true);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--disable-extensions");
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.addArguments("--window-size=1280,1024");
WebDriver driver = new ChromeDriver(options);
driver.getTitle();
WebDriverRunner.setWebDriver(driver);
}
}
private void savedownloadsintodedirectory(字符串downloadFolderName){
if(Configuration.browser.equals(“chrome”)){
HashMap chromePrefs=新HashMap();
chromePrefs.put(“profile.default\u content\u settings.popups”,0);
//这必须是canonicalPath,否则它在Windows上无法工作。
试一试{
chromePrefs.put(“download.default_目录”,新文件(downloadFolderName.getCanonicalPath());
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
chromePrefs.put(“下载.提示下载”,false);
chromePrefs.put(“download.directory\u upgrade”,true);
chromePrefs.put(“safebrowsing.enabled”,true);
//这花了很长时间才找到。
chromePrefs.put(“安全浏览。禁用下载保护”,true);
ChromeOptions选项=新的ChromeOptions();
选项。设置实验选项(“prefs”,chromePrefs);
options.addArguments(“--disable extensions”);
选项。添加参数(“--headless”);
options.addArguments(“--no sandbox”);
options.addArguments(“--windowsize=12801024”);
WebDriver=新的ChromeDriver(选项);
driver.getTitle();
WebDriverRunner.setWebDriver(驱动程序);
}
}
我认为修复它的正确方法可能是WebDriverProvider。。但我没有看到它的好处。这就是它目前的状况。下面的配置参数的复制不是最好的方法
package core;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.WebDriverProvider;
public class SaveFileWebDriverProviderShared implements WebDriverProvider {
@Override
public WebDriver createDriver(DesiredCapabilities capabilities) {
System.out.println("CLE headless" + Configuration.headless);
System.out.println(Configuration.browserSize);
DesiredCapabilities dc2 = Configuration.browserCapabilities;
Set<String> test= dc2.getCapabilityNames();
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
//THIS must be the canonicalPath,otherwise it won't work on Windows.
// try {
// chromePrefs.put("download.default_directory", new File("/tmp/downloadTest",afs.getActiveSuite()) );
File downloadFolder = new File("/tmp/downloadTest/CLETEst");
downloadFolder.mkdir();
//THIS must be the canonicalPath,otherwise it won't work on Windows.
try {
chromePrefs.put("download.default_directory", downloadFolder.getCanonicalPath() );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
chromePrefs.put("download.prompt_for_download", false);
chromePrefs.put("download.directory_upgrade", true);
chromePrefs.put("safebrowsing.enabled", true);
//THIS took forever to find.
chromePrefs.put("safebrowsing.disable_download_protection",true);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--disable-extensions");
options.addArguments("--no-sandbox");
if(Configuration.headless) {
options.addArguments("--headless");
}
if(Configuration.browserSize != null) {
options.addArguments("--window-size="+Configuration.browserSize.replace('x', ','));
}
options.merge(capabilities);
ChromeDriver driver = new ChromeDriver(options);
return driver;
}
}
包核;
导入java.io.File;
导入java.io.IOException;
导入java.util.HashMap;
导入java.util.Set;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.chrome.ChromeOptions;
导入org.openqa.selenium.remote.DesiredCapabilities;
导入com.codeborne.selenide.Configuration;
导入com.codeborne.selenide.WebDriverProvider;
公共类SaveFileWebDriverProviderShared实现WebDriverProvider{
@凌驾
公共WebDriver createDriver(DesiredCapabilities){
系统输出打印项次(“CLE无头”+配置无头);
System.out.println(配置.browserSize);
DesiredCapabilities dc2=Configuration.browserCapabilities;
Set test=dc2.getCapabilityNames();
HashMap chromePrefs=新HashMap();
chromePrefs.put(“profile.default\u content\u settings.popups”,0);
//这必须是canonicalPath,否则它在Windows上无法工作。
//试一试{
//chromePrefs.put(“download.default_目录”,新文件(“/tmp/downloadTest”,afs.getActiveSuite());
File downloadFolder=新文件(“/tmp/downloadTest/CLETEst”);
downloadFolder.mkdir();
//这必须是canonicalPath,否则它在Windows上无法工作。
试一试{
chromePrefs.put(“download.default_目录”,downloadFolder.getCanonicalPath());
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
chromePrefs.put(“下载.提示下载”,false);
chromePrefs.put(“download.directory\u upgrade”,true);
chromePrefs.put(“safebrowsing.enabled”,true);
//这花了很长时间才找到。
chromePrefs.put(“安全浏览。禁用下载保护”,true);
ChromeOptions选项=新的ChromeOptions();
选项。设置实验选项(“prefs”,chromePrefs);
options.addArguments(“--disable extensions”);
options.addArguments(“--no sandbox”);
if(无头配置){
选项。添加参数(“--headless”);
}
if(Configuration.browserSize!=null){
options.addArguments(“--window size=“+Configuration.browserSize.replace('x',','));
}
选项。合并(功能);
ChromeDriver驱动程序=新的ChromeDriver(选项);
返回驱动器;
}
}
技术细节:
启动ChromeDriver 78.0.3904.11(eaaae9de6b8999773fa33f92ce1e1bbe294437cf参考/分支机构负责人/3904{86})<