How to initiate a Mozilla Browsing session through FirefoxProfile?

做~自己de王妃 提交于 2019-12-11 09:02:29

问题


I am trying to use Firefox Profiling. But it is throwing an error at below line in the code. Please see attached snapshot

Please, can someone assist?

Code : -

WebDriver driver = new FirefoxDriver(prof);

Error: -->

The constructor FirefoxDriver(FirefoxProfile) is undefined

Below versions I am using:-

  • Selenium--> 3.12.0
  • Firefox Setup 50.0

Codes:

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

回答1:


There isn't such constructor that gets the profile and creates the driver. That's what the exception is telling you. You can see the javadoc here:

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

You can try something like that:

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



回答2:


If you look at the Java Docs of FirefoxDriver Class in Selenium Java Client v3.13.0 the valid constructors are as follows:

  • FirefoxDriver()
  • FirefoxDriver(FirefoxOptions options)
  • FirefoxDriver(GeckoDriverService service)
  • FirefoxDriver(GeckoDriverService service, FirefoxOptions options)
  • FirefoxDriver(XpiDriverService service)
  • FirefoxDriver(XpiDriverService service, FirefoxOptions options)

So clearly, as per your code trials the following line of code is not a valid option:

WebDriver driver = new FirefoxDriver(prof);

Hence you see the error as:

The constructor FirefoxDriver(FirefoxProfile) is undefined

The auto suggestions are as follows:

Solution

As a solution:

  • Either you can convert the instance of FirefoxProfile into DesiredCapabilities() type object and then merge() into an FirefoxOptions() type object as follows:

    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);
    
  • Or you can straightaway use the setProfile() method through an instance of FirefoxOptions() as follows:

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

Note: To use an existing Firefox Profile for your Test Execution first you have to create a Firefox Profile manually following the instructions at Creating a new Firefox profile on Windows.



来源:https://stackoverflow.com/questions/51583522/how-to-initiate-a-mozilla-browsing-session-through-firefoxprofile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!