如何在Java中使用Selenium的Firefox配置文件和Firefox选项。

huangapple go评论80阅读模式
英文:

How to use both the Firefox Profile and Firefox options with Selenium in Java

问题

我正在编写一个测试,我想要使用无头模式,并且还将使用 Selenium 在 Java 中下载文件。从这里我了解到,您可以通过在初始化驱动程序之前插入以下代码将驱动程序设置为无头模式:

options.setHeadless(true); //将驱动程序设置为无头模式
WebDriver driver = new FirefoxDriver(options);

而且我可以使用这个方法来编写一个 Firefox 配置文件,该配置文件将指定下载目录,并允许我在 Firefox 中下载文件而无需任何弹出窗口(我已经修改了该方法,以允许将下载位置作为参数传递给该方法)。创建完该方法后,我可以在主函数中像这样调用它:

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) 更正为 setHeadless(true)

英文:

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:

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). After creating the method, I call it within main like this:

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.

How can I combine these two funcitons, the profile and the options?

edit: fixed the setHeadless(false) to be setHedless(true)

答案1

得分: 3

使用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");

使用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");

使用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");

参考资料

您可以在以下链接中找到一些相关的详细讨论:

英文:

To use a new Firefox Profile through FirefoxOptions you can use the following code block:

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:

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:

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");

References

You can find a couple of relevant detailed discussions in:

答案2

得分: 1

options.setHeadless(false) 函数应该有一个参数为 true,而不是 false

英文:

The function options.setHeadless(false) should have a true parameter not false

huangapple
  • 本文由 发表于 2020年8月19日 06:41:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63477601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定