如何在Java中将Firefox配置文件和Firefox选项与Selenium一起使用 [英] How to use both the Firefox Profile and Firefox options with Selenium in Java

查看:60
本文介绍了如何在Java中将Firefox配置文件和Firefox选项与Selenium一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个无头的测试,该测试还将使用Selenium在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:

options.setHeadless(true); //sets driver to work headless 
WebDriver driver = new FirefoxDriver(options);

,我可以使用此方法编写一个Firefox个人资料,该个人资料将规定一个下载目录,并允许我使用不带任何弹出窗口的Firefox下载文件(我已修改该方法以允许该方法以允许下载位置作为参数).创建方法之后,我在main中这样调用它:

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

我要么没有运行的无头版本的Firefox,要么在下载zip文件时收到弹出的保存提示.

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:将 setHeadless(false)修复为 setHedless(true)

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

推荐答案

要使用新的

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

要使用新的 Firefox配置文件通过 FirefoxOptions 以及

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:

这篇关于如何在Java中将Firefox配置文件和Firefox选项与Selenium一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆