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

2024-05-06

我正在编写一个我想要无头的测试,它还将使用 Selenium 在 java 中下载文件。从here https://stackoverflow.com/questions/63143518/org-openqa-selenium-timeoutexception-connection-refused-error-while-running-hea我了解到,您可以在初始化驱动程序之前通过抛出以下代码将驱动程序设置为无头:

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

我可以用这个method https://www.seleniumeasy.com/selenium-tutorials/how-to-download-a-file-with-webdriver#:%7E:text=To%20handle%20Downloads%20with%20selenium,%20we%20need%20to,check%20if%20the%20file%20is%20downloaded%20or%20not.编写一个 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) to be setHedless(true)


要使用新的火狐简介 https://stackoverflow.com/questions/54661217/unknown-error-messageconnection-refused-stacktrace-while-trying-to-use/54664172#54664172通过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");

使用现有的火狐简介 https://stackoverflow.com/questions/52464598/how-can-i-set-a-default-profile-for-the-firefox-driver-in-selenium-webdriver-3/52474787#52474787通过FirefoxOptions您可以使用以下代码块:

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

要使用新的火狐简介 https://stackoverflow.com/questions/53886741/selenium-tests-take-several-minutes-to-start-when-loading-a-profile/54072337#54072337通过FirefoxOptions随着优先 https://stackoverflow.com/questions/44074656/how-to-download-csv-file-through-firefox-profile-in-java/44084814#44084814您可以使用以下代码块:

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

参考

您可以在以下位置找到一些相关的详细讨论:

  • 无法解析构造函数 FirefoxDriver(org.openqa.selenium.firefox.FirefoxProfile) https://stackoverflow.com/questions/49683355/cannot-resolve-constructor-firefoxdriverorg-openqa-selenium-firefox-firefoxprof/49686604#49686604
  • 无法在网络驱动程序中传递 FirefoxProfile 参数以使用首选项下载文件 https://stackoverflow.com/questions/47568275/unable-to-pass-firefoxprofile-parameter-in-webdriver-to-use-preferences-to-downl/47569170#47569170
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐