There are currently two recommended ways to launch the latest version of Selenium using a specific profile. One is through the Desired Capabilities object and the other us by using Firefox Options.
Desired Capabilities:
public static WebDriver launchFirefoxProfileByFFCapabilities()
{
WebDriver driver = null;
System.setProperty("webdriver.gecko.driver", "PATH");
ProfilesIni profile = new ProfilesIni();
String profileName = "Profile_1";
FirefoxProfile firefoxProfileByProfilesIni = profile.getProfile(profileName);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, firefoxProfileByProfilesIni);
FirefoxOptions opt = new FirefoxOptions();
opt.merge(dc);
driver = new FirefoxDriver(opt);
return driver;
}
Firefox Options:
public static WebDriver launchFirefoxProfileByFFOptions()
{
WebDriver driver = null;
System.setProperty("webdriver.gecko.driver", "PATH");
ProfilesIni profile = new ProfilesIni();
String profileName = "Profile_1";
FirefoxProfile firefoxProfileByProfilesIni = profile.getProfile(profileName);
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(firefoxProfileByProfilesIni);
driver = new FirefoxDriver(opt);
return driver;
}
I noticed a few interesting discrepancies between the two approaches and I'm trying to understand what is the reason for these discrepancies and how to prevent them.
1) With Firefox Options when the specified Profile Name does not exist (as it appears in the Firefox - Choose User Profile dialog) throws WebDriverException: Unexpected value for profile: null. However with Desired Capabilities no such exception is thrown and Selenium silently ignores the profile preference (which is not ideal)
2) If the Profile Name exists (as it appears in the Firefox - Choose User Profile dialog) but the path in profiles.ini file is incorrect even Firefox Options will launch silently ignore the profile preference and launch WebDriver not connected to any profile
What is the cause of these discrepancies? How can they be prevented?
Thanks
It seems that Capabilities and FirefoxProfile/FirefoxOptions handle the profileIni in a different way.
Could you please modify and add the stack-traces so we can further help you?
Related
I'd like to know if the method shared here: Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection it's still currently working and if yes I'd like to know how to do the same using Java on geckodriver.
Since now I tried the following without success:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("dom.webdriver.enabled","False");
profile.setPreference("useAutomationExtension","False");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
String user = System.getProperty("user.name");
System.setProperty("webdriver.gecko.driver", "/Users/" + user + "/Desktop/Configs/geckodriver");
FirefoxDriver driver = new FirefoxDriver(options);
driver.executeScript("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})");
driver.get("https://mysite/");
But typing navigator.webdriver in console still returns True
I want to run the headless browser and below is the code for same.
However when i ran it. it shows "Error: no DISPLAY environment variable specified"
try {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("webdriver.log.driver", "INFO");
profile.setPreference("webdriver.log.file", targetDir + File.separator + "firefoxSeleniumServer.log");
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv;text/plain");
if(platform.equalsIgnoreCase("linux")) {
DesiredCapabilities desiredCap = DesiredCapabilities.firefox();
profile.setPreference("browser.download.dir",System.getProperty("user.dir")+ File.separator + "target");
System.setProperty("webdriver.gecko.driver", "/test/geckodriver/geckodriver");
System.setProperty("webdriver.firefox.bin","/usr/bin/firefox/firefox");
desiredCap.setCapability("headless", true);
driver = new FirefoxDriver();
}
However when i set the display it shows unable to open firefox on DISPLAY:99
Also i tried setting the xvfb as well. But that also did not work.
As i am using gekco driver here, do i need to do some more configurations.
I think you are supposed to provide the DesiredCapabilities object as a parameter to the FirefoxDriver constructor:
References
The javadoc for FirefoxDriver.
What is the use of DesiredCapabilities in Selenium WebDriver?
Below code didn't changed FF profile when I am having 3(randomly created nos.) custom FF profiles and 1 default.
WebDriver driver;
String profilepath = "<user Dir>/Local/Mozilla/Firefox/Profiles/";
ProfilesIni profilesini = new ProfilesIni();
FirefoxProfile firefoxprofile = new FirefoxProfile(new File(profilepath));
firefoxprofile = profilesini.getProfile("profile_1");
driver = new FirefoxDriver(firefoxprofile);
FF profiles are : profile_1, profile_2 and profile_3. When I run the code FF launched with either 'profile_2 or default'. Not with passed profile name (profile_1)
Am using selenium-sever standalone 2.53.0 and FF 46.0 on Win 10, 64 bit.
If you want to launch a custom firefox profile. This is how you do it.
First you create an object of ProfilesIni then you get the desired firefox profile using getProfile() and then that particular profile is passed to WebDriver initialization.
ProfilesIni profiles = new ProfilesIni();
FirefoxProfile profile = profiles.getProfile("profile_1");
WebDriver driver = new FirefoxDriver(profile);
Hope it helps!
I am currently trying to write a test that tests the non-JS version of my website. I use Selenium with Java. I have tried this:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(profile);
driver.get();
However this isn't working. It just loads the page with JavaScript Enabled.
As a workaround I did, this, for the requirement.
It will manually set the javascript.enabled property to false by the following script.
WebDriver driver = new FirefoxDriver();
driver.get("about:config");
Actions act = new Actions(driver);
act.sendKeys(Keys.RETURN).sendKeys("javascript.enabled").perform();
act.sendKeys(Keys.TAB).sendKeys(Keys.RETURN).perform();
How can I click accept/dismiss unhandledalerts with WebDriver?
Is it possible to check where the unhandled alerts are coming?
How can I use UNEXPECTED_ALERT_BEHAVIOUR capability? It is not working as I expected?
I've tried like this
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, "Accept");
Try this code:
WebDriver driver;
#BeforeClass
public void setUp() {
DesiredCapabilities dc=new DesiredCapabilities();
dc.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,UnexpectedAlertBehaviour.ACCEPT);
driver =new FirefoxDriver(dc);
}
I hope this would be helpful to you.
Open Firefox and try 'about:config'.
Findout which Preference Name can solve you problem and set values accordingly using below code. example: I am disabling automatic update of Firefox.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("app.update.enabled", false);
WebDriver driver = new FirefoxDriver(profile);