Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中与Selenium一起使用Firefox配置文件和Firefox选项_Java_Selenium_Firefox_Headless_Selenium Firefoxdriver - Fatal编程技术网

如何在Java中与Selenium一起使用Firefox配置文件和Firefox选项

如何在Java中与Selenium一起使用Firefox配置文件和Firefox选项,java,selenium,firefox,headless,selenium-firefoxdriver,Java,Selenium,Firefox,Headless,Selenium Firefoxdriver,我正在编写一个测试,我想使用headless,它还将使用Selenium在java中下载一个文件。从中我了解到,在初始化驱动程序之前,可以通过抛出以下代码将驱动程序设置为无头: options.setHeadless(true); //sets driver to work headless WebDriver driver = new FirefoxDriver(options); 我可以用它来编写一个Firefox配置文件,它将指定一个下载目录,并允许我下载一个带有Firefox的文件,

我正在编写一个测试,我想使用headless,它还将使用Selenium在java中下载一个文件。从中我了解到,在初始化驱动程序之前,可以通过抛出以下代码将驱动程序设置为无头:

options.setHeadless(true); //sets driver to work headless 
WebDriver driver = new FirefoxDriver(options);
我可以用它来编写一个Firefox配置文件,它将指定一个下载目录,并允许我下载一个带有Firefox的文件,而不需要任何弹出窗口(我修改了该方法,允许该方法允许下载位置作为参数)。创建方法后,我在main中调用它,如下所示:

downloadPath = "C:\Scripts"
WebDriver driver = new FirefoxDriver(FirefoxDriverProfile(downloadPath));
然后说我想将以下代码与上述两种方法之一结合使用:

driver.get(https://github.com/mozilla/geckodriver/releases);
driver.findElement(By.linkText("geckodriver-v0.27.0-win64.zip")).click();
我要么不运行firefox的无头版本,要么在下载zip文件时得到一个弹出保存提示

如何组合这两个函数,配置文件和选项


编辑:修复了
setHeadless(false)
setHedless(true)

函数
选项。setHeadless(false)
应具有
true
参数not
false
要通过
FirefoxOptions
使用新的代码块,您可以使用以下代码块:

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(new FirefoxProfile());
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.com");
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(testprofile);
WebDriver driver =  new FirefoxDriver(opt);
driver.get("https://www.google.com");
String downloadFilepath = "C:\\path\\to\\MozillaFirefoxDownload";
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir",downloadFilepath);
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
options.setProfile(profile);
WebDriver driver =  new FirefoxDriver(options);
driver.get("https://www.google.com");
要通过
FirefoxOptions
使用现有代码块,可以使用以下代码块:

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(new FirefoxProfile());
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.com");
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(testprofile);
WebDriver driver =  new FirefoxDriver(opt);
driver.get("https://www.google.com");
String downloadFilepath = "C:\\path\\to\\MozillaFirefoxDownload";
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir",downloadFilepath);
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
options.setProfile(profile);
WebDriver driver =  new FirefoxDriver(options);
driver.get("https://www.google.com");
要通过FirefoxOptions
使用新代码,您可以使用以下代码块:

System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(new FirefoxProfile());
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.com");
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(testprofile);
WebDriver driver =  new FirefoxDriver(opt);
driver.get("https://www.google.com");
String downloadFilepath = "C:\\path\\to\\MozillaFirefoxDownload";
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir",downloadFilepath);
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
options.setProfile(profile);
WebDriver driver =  new FirefoxDriver(options);
driver.get("https://www.google.com");

工具书类 您可以在以下内容中找到一些相关的详细讨论:


噢,见鬼,谢谢你抓住了。尽管如此,我仍然只能用选项或个人资料给司机打电话,我不知道如何同时打电话。