[英]How to use both the Firefox Profile and Firefox options with Selenium in Java
I'm writing a test that I'd like to headless, which will also download a file within java using Selenium. from here I learn that you can set a driver to be headless by throwing this code before you initialize the driver:我正在编写一个我想要无头的测试,它还将使用 Selenium 下载 java 内的文件。从这里我了解到,您可以通过在初始化驱动程序之前抛出此代码来将驱动程序设置为无头:
options.setHeadless(true); //sets driver to work headless
WebDriver driver = new FirefoxDriver(options);
and that I can use this method to write a Firefox Profile which will dictate a download directory and allow me to download a file with firefox w/o any pop up windows (I've modified the method to allow the method to permit the download location as an argument).并且我可以使用此方法编写一个 Firefox 配置文件,它将指定一个下载目录并允许我下载一个文件 firefox w/o 任何弹出窗口 windows(我修改了该方法以允许该方法允许下载位置作为论据)。 After creating the method, I call it within main like this:
创建方法后,我在 main 中调用它,如下所示:
downloadPath = "C:\Scripts"
WebDriver driver = new FirefoxDriver(FirefoxDriverProfile(downloadPath));
and then say I want to use the following code with either of the two methods above:然后说我想将以下代码与上述两种方法之一一起使用:
driver.get(https://github.com/mozilla/geckodriver/releases);
driver.findElement(By.linkText("geckodriver-v0.27.0-win64.zip")).click();
I'll either not have a headless version of firefox running, or I get a pop up save prompt when I go to download the zip file.我要么没有运行 firefox 的无头版本,要么在我 go 下载 zip 文件时弹出保存提示。
How can I combine these two funcitons, the profile and the options?我怎样才能结合这两个功能,配置文件和选项?
edit: fixed the setHeadless(false)
to be setHedless(true)
编辑:将
setHeadless(false)
固定为setHedless(true)
To use a new Firefox Profile through FirefoxOptions
you can use the following code block:要通过
FirefoxOptions
使用新的Firefox 配置文件,您可以使用以下代码块:
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");
To use an existing Firefox Profile through FirefoxOptions
you can use the following code block:要通过
FirefoxOptions
使用现有的Firefox 配置文件,您可以使用以下代码块:
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");
To use an new Firefox Profile through FirefoxOptions
along with preferences you can use the following code block:要通过
FirefoxOptions
使用新的Firefox 配置文件以及首选项,您可以使用以下代码块:
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");
You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:
The function options.setHeadless(false)
should have a true
parameter not false
function
options.setHeadless(false)
应该有一个true
参数而不是false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.