简体   繁体   English

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

[英]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");

References参考

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.

相关问题 如何在 Selenium 中使用自定义 Firefox 配置文件? (Java) (并通过 HTML 授权窗口) - How to use custom Firefox Profile with Selenium? (Java) (And pass HTML Authorization Window) 如何使用Java在Selenium Webdriver中为Firefox配置文件使用相对文件路径? - How to use relative file path for firefox profile in Selenium webdriver using java? 无法在Selenium中使用带有Firefox配置文件的PhantomJS驱动程序 - Unable to use PhantomJS driver with Firefox profile in Selenium Selenium在Chrome和Firefox中均无法在Java中运行 - Selenium not working in Java, both Chrome and Firefox 如何使用Selenium 2 Webdriver打开指定的配置文件Firefox? - How to open specified profile Firefox with Selenium 2 Webdriver? Firefox配置文件中的Selenium禁用插件 - Selenium disable plugins in firefox profile 在Selenium Java中需要这些(Firefox)浏览器配置文件设置的chrome等价物 - Need the chrome equivalent of these (Firefox) browser profile settings in Selenium Java 将 Firefox 配置文件设置为使用 Selenium 和 Java 自动下载文件 - Set Firefox profile to download files automatically using Selenium and Java Selenium:使用 Java 代码使用默认配置文件打开 Firefox - Selenium: Open Firefox with default profile using java code Java-无法在Firefox浏览器中使用Selenium网格 - Java - Unable to use selenium grid with firefox browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM