问题
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
intoDesiredCapabilities()
type object and thenmerge()
into anFirefoxOptions()
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 ofFirefoxOptions()
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