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

Java 如何通过FirefoxProfile启动Mozilla浏览会话?,java,selenium,firefox,selenium-webdriver,geckodriver,Java,Selenium,Firefox,Selenium Webdriver,Geckodriver,我正在尝试使用Firefox评测。但它在代码的下面一行抛出了一个错误。请参阅附加的快照 请问有人能帮忙吗 代码:- WebDriver driver = new FirefoxDriver(prof); 错误:--> 构造函数FirefoxDriver(FirefoxProfile)未定义 我使用的以下版本:- 硒-->3.12.0 Firefox安装程序50.0 代码: import java.util.concurrent.TimeUnit; import org.openqa.

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

请问有人能帮忙吗

代码:-

WebDriver driver = new FirefoxDriver(prof);

错误:-->

构造函数FirefoxDriver(FirefoxProfile)未定义

我使用的以下版本:-

  • 硒-->3.12.0
  • Firefox安装程序50.0
代码:

 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:

您可以尝试类似的方法:

FirefoxOptions options = new FirefoxOptions();
options.setProfile(yourProfile);
FirefoxDriver driver = new FirefoxDriver(options);
如果您查看Selenium Java Client v3.13.0中类的Java文档,有效的构造函数如下所示:

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);
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("Abhi_Selenium");
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(testprofile);
WebDriver driver =  new FirefoxDriver(opt);
显然,根据您的代码试用,以下代码行不是有效选项:

WebDriver driver = new FirefoxDriver(prof);
因此,您将错误视为:

The constructor FirefoxDriver(FirefoxProfile) is undefined
自动建议如下:

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);
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("Abhi_Selenium");
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(testprofile);
WebDriver driver =  new FirefoxDriver(opt);

解决方案 作为解决方案:

  • 您可以将
    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);
    
    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile testprofile = profile.getProfile("Abhi_Selenium");
    FirefoxOptions opt = new FirefoxOptions();
    opt.setProfile(testprofile);
    WebDriver driver =  new FirefoxDriver(opt);
    
  • 或者,您可以通过
    FirefoxOptions()
    的实例直接使用
    setProfile()
    方法,如下所示:

    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);
    
    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile testprofile = profile.getProfile("Abhi_Selenium");
    FirefoxOptions opt = new FirefoxOptions();
    opt.setProfile(testprofile);
    WebDriver driver =  new FirefoxDriver(opt);
    

注意:要使用现有的Firefox配置文件执行测试,首先必须按照中的说明手动创建Firefox配置文件。

感谢您查看我的问题我猜这是我答案的一个更漂亮的版本