如何通过 FirefoxProfile 启动 Mozilla 浏览会话?

How to initiate a Mozilla Browsing session through FirefoxProfile?

我正在尝试使用 Firefox Profiling。但它在代码的下面一行抛出错误。请参阅随附的快照

拜托,有人可以帮忙吗?

代码:-

WebDriver driver = new FirefoxDriver(prof);

错误:-->

The constructor FirefoxDriver(FirefoxProfile) is undefined

我正在使用以下版本:-

代码:

 import java.util.concurrent.TimeUnit;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.firefox.FirefoxDriver;
 import org.openqa.selenium.firefox.FirefoxProfile;
 import org.openqa.selenium.firefox.internal.ProfilesIni;
 public class Gmail {

    public static void main(String[] args) {
       System.setProperty("webdriver.gecko.driver", "D:\Drivers\geckodriver.exe");

       ProfilesIni allProf = new ProfilesIni();// all profiles
       FirefoxProfile prof = allProf.getProfile("Abhi_Selenium");

       WebDriver driver = new FirefoxDriver(prof);
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       driver.get("http://gmail.com");

没有这样的构造函数可以获取配置文件并创建驱动程序。这就是异常告诉你的。您可以在此处查看 javadoc:

https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/firefox/FirefoxDriver.html

您可以尝试类似的方法:

FirefoxOptions options = new FirefoxOptions();
options.setProfile(yourProfile);
FirefoxDriver driver = new FirefoxDriver(options);

如果您在 Selenium Java 客户端中查看 FirefoxDriver Class 的 Java 文档 v3.13.0有效的构造函数如下:

很明显,根据您的代码试验,以下代码行不是有效选项:

WebDriver driver = new FirefoxDriver(prof);

因此您看到错误为:

The constructor FirefoxDriver(FirefoxProfile) is undefined

自动提示如下:

解决方案

作为解决方案:

  • 或者你可以将FirefoxProfile的实例转换成DesiredCapabilities()类型的对象,然后merge()转换成FirefoxOptions()类型的对象,如下:

    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile testprofile = profile.getProfile("Abhi_Selenium");
    DesiredCapabilities dc = DesiredCapabilities.firefox();
    dc.setCapability(FirefoxDriver.PROFILE, testprofile);
    FirefoxOptions opt = new FirefoxOptions();
    opt.merge(dc);
    WebDriver driver =  new FirefoxDriver(opt);
    
  • 或者您可以通过 FirefoxOptions() 的实例直接使用 setProfile() 方法,如下所示:

    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile testprofile = profile.getProfile("Abhi_Selenium");
    FirefoxOptions opt = new FirefoxOptions();
    opt.setProfile(testprofile);
    WebDriver driver =  new FirefoxDriver(opt);
    

注意:要将现有的 Firefox 配置文件 用于您的 测试执行,首先您需要按照 Creating a new Firefox profile on Windows.

中的说明手动创建 Firefox 配置文件