【问题标题】:How to initiate a Mozilla Browsing session through FirefoxProfile?如何通过 FirefoxProfile 启动 Mozilla 浏览会话?
【发布时间】:2018-07-29 18:54:02
【问题描述】:

我正在尝试使用 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");

【问题讨论】:

    标签: java selenium firefox selenium-webdriver geckodriver


    【解决方案1】:

    没有这样的构造函数可以获取配置文件并创建驱动程序。这就是异常告诉你的。您可以在此处查看 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);
    

    【讨论】:

    • 感谢您查看我的问题
    【解决方案2】:

    如果您查看 Selenium Java Client v3.13.0FirefoxDriver 类的Java Docs,则有效的构造函数如下:

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

    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 配置文件 用于您的测试执行,首先您必须创建一个 Firefox 配置文件 手动按照Creating a new Firefox profile on Windows 的说明进行操作。

    【讨论】:

    • 我想这是我的答案更漂亮的版本
    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 2023-03-10
    • 2010-11-27
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    相关资源
    最近更新 更多