Selenium cannot load specific created profile in firefox driver - c#

I am trying to get a specific firefox profile which I created beforehand.
However when i execute the below code i get an exception saying that the profile doesn't exist.
var profileManager = new FirefoxProfileManager();
var profile = profileManager.GetProfile("profile");
var options = new FirefoxOptions { Profile = profile };
profile.SetPreference("webdriver.firefox.profile", "profile");
var driver = new FirefoxDriver(#"C:\Users\danza\source\repos\InstaManager\", options);

So after investigating this problem, I found out that it was mainly a package version issue. I was using Selenium.WebDriver alpha version nuget package. The solution was to downgrade to a stable version of this nuget package.

Alternatively you can use it so
var options = new FirefoxOptions();
options.Profile = new FirefoxProfile("C:\Users\username\AppData\Roaming\Mozilla\Firefox\Profiles\profilename");
var webDriver = new FirefoxDriver(webdriverPath, options)
The firefox profiles are stored in the path AppData\Roaming\Mozilla\Firefox\Profiles

Related

Unable to get Chrome Performance logs in Selenium C#

I am using following nuget packages in my solution
Selenium.WebDriver - v3.141.0
Selenium.WebDriver.ChromeDriver - v79.0.3945.3600
using following code I am creating a Chrome driver instance
ChromeOptions options = new ChromeOptions();
//Get Performance Logs from Network tab
ChromePerformanceLoggingPreferences perfLogPrefs = new ChromePerformanceLoggingPreferences();
options.PerformanceLoggingPreferences = perfLogPrefs;
options.SetLoggingPreference("performance", LogLevel.All);
(or)
ChromePerformanceLoggingPreferences perfLogPrefs = new
ChromePerformanceLoggingPreferences();
perfLogPrefs.AddTracingCategories(new string[] { "devtools.timeline" });
options.PerformanceLoggingPreferences = perfLogPrefs;
options.SetLoggingPreference("goog:loggingPrefs", LogLevel.All);
options.AddAdditionalCapability(CapabilityType.EnableProfiling, true, true);
and combining with this
options.AddUserProfilePreference("intl.accept_languages", "en-US");
options.AddUserProfilePreference("disable-popup-blocking", "true");
options.AddArgument("test-type");
options.AddArgument("--disable-gpu");
options.AddArgument("no-sandbox");
options.AddArgument("start-maximized");
options.LeaveBrowserRunning = true;
IWebDriver driver = new ChromeDriver(options);
but while creating Chrome driver instance, I am getting following error message
invalid argument: entry 0 of 'firstMatch' is invalid
from invalid argument: perfLoggingPrefs specified, but performance logging was not enabled
May I know what changes do I need to make please to get the performance logs with latest version of Chrome and Selenium driver
I am able to retrieve Performance Logs using the below code when I was using lower versions of Chrome driver (2.35.0)
var logs = driver.Manage().Logs.GetLog("performance");
for (int i = 0; i < logs.Count; i++)
{
Console.WriteLine(logs[i].Message);
}
With Selenium WebDriver (v4.0.0-alpha04) and Selenium.Chrome.WebDriver (v79.0.0) and using the following code, I am able to retrieve the performance logs.
ChromeOptions options = new ChromeOptions();
//Following Logging preference helps in enabling the performance logs
options.SetLoggingPreference("performance", LogLevel.All);
//Based on your need you can change the following options
options.AddUserProfilePreference("intl.accept_languages", "en-US");
options.AddUserProfilePreference("disable-popup-blocking", "true");
options.AddArgument("test-type");
options.AddArgument("--disable-gpu");
options.AddArgument("no-sandbox");
options.AddArgument("start-maximized");
options.LeaveBrowserRunning = true;
//Creating Chrome driver instance
IWebDriver driver = new ChromeDriver(options);
//Extracting the performance logs
var logs = driver.Manage().Logs.GetLog("performance");
for (int i = 0; i < logs.Count; i++)
{
Console.WriteLine(logs[i].Message);
}
Hope this helps.
All these days, I was using the following two lines with latest versions of Selenium WebDriver and Chrome Driver and couldn't figure out what the issue was and now with the latest versions, the following two lines of code is not required.
var perfLogPrefs = new ChromePerformanceLoggingPreferences();
options.PerformanceLoggingPreferences = perfLogPrefs;

Can I control selenium with firefox portable?

I have read a lot of articles related to using selenium and portable firefox. But the code doesn't work with me. Did I do something wrong? I am using firefox portable 67 and Geckodriver v0.24.0.
FirefoxDriverService driverService = FirefoxDriverService.CreateDefaultService(#"D:\C# Project\FirefoxPortal tesst\FirefoxPortal tesst\bin\Debug\", "geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
//var profile = new FirefoxProfileManager().GetProfile("");
options.Profile = new FirefoxProfile(#"D:\C# Project\FirefoxPortal tesst\FirefoxPortal tesst\bin\Debug\FirefoxPortable\Data\profile");
options.BrowserExecutableLocation = #"D:\C# Project\FirefoxPortal tesst\FirefoxPortal tesst\bin\Debug\FirefoxPortable\FirefoxPortable.exe";
IWebDriver driver = new FirefoxDriver(driverService, options);
driver.Navigate().GoToUrl("https://stackoverflow.com/");
I do not have time today but try changing
options.BrowserExecutableLocation = #"D:\C# Project\FirefoxPortal tesst\FirefoxPortal tesst\bin\Debug\FirefoxPortable\FirefoxPortable.exe";
to
options.BrowserExecutableLocation = #"D:\C# Project\FirefoxPortal tesst\FirefoxPortal tesst\bin\Debug\FirefoxPortable\App\firefox64\firefox.exe";
or to wherever you have your firefox.exe file.

FireFox & Selenium not working on specific version

In a project, we have exact guidelines, which Selenium & FireFox Versions to run for UI Tests:
- FireFox: 33.1 (some have 33.1.1, which works also)
- NuGet Selenium.WebDriver 3.3.0
- NuGet Selenium.Support 3.3.0
The FireFoxWebDriver is initialized like this:
var firefoxDirectory = #"C:\Program Files (x86)\Mozilla Firefox\";
var driverExecutableFileName = "firefox.exe";
var profileManager = new FirefoxProfileManager();
var profile = profileManager.GetProfile("default");
profile.EnableNativeEvents = false;
profile.SetPreference("intl.accept_languages", "en-US");
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.dir", "C:\\Temp");
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream");
var defaultPath = $"{firefoxDirectory}{driverExecutableFileName}";
var options = new FirefoxOptions
{
Profile = profile,
UseLegacyImplementation = true
};
var service = FirefoxDriverService.CreateDefaultService(firefoxDirectory, driverExecutableFileName);
if (File.Exists(defaultPath))
{
options.BrowserExecutableLocation = defaultPath;
}
var fireFoxDriver = new FirefoxDriver(service, options, TimeSpan.FromSeconds(30));
return fireFoxDriver;
My problem: It works on every other developer machine, but on mine, the following happens:
As soon as
var fireFoxDriver = new FirefoxDriver(service, options, TimeSpan.FromSeconds(30));
Is hit, an empty FireFox window opens, but then it stops until the Timeout is reached. The length of the timeout doesn't matter, Selenium just doesn't seem to connect.
I uninstalled FireFox, the NuGet cache etc., imported the default-profile from other developers and checked all topics regarding that problem, but most topics are related to version incompatibility, which can't be the problem, since other devs have the same environment.
Are there other known issues or possibilities, what on my machine could influence this behavior?
add this two lines to configuration
profile.SetPreference("browser.startup.homepage_override.mstone", "ignore");
profile.SetPreference("startup.homepage_welcome_url.additional", "about:blank");

Selenium.WebDriver with portable FireFox c#

I need to start Selenium with Firefox Portable.
If I start Firefox.exe portable with doublé clic, it starts.
The path to Firefox.exe is correct: A FireFoxPortable folder inside Debug project's folder.
This is the code I use:
var driverService = FirefoxDriverService.CreateDefaultService();
driverService.FirefoxBinaryPath =
Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"FireFoxPortable",
"FireFox.exe");
driverService.HideCommandPromptWindow = true;
driverService.SuppressInitialDiagnosticInformation = true;
var options = new FirefoxOptions();
var driver = new FirefoxDriver(options);
Creating the driver I have an exception -> Cannot find Firefox binary in PATH or default install locations. Make sure Firefox is installed. OS appears to be: Vista
I try this variant, but no work:
var driver = new FirefoxDriver(driverService);
I'm using this nuget packages:
Is this the correct way?
Thanks for your time!
UPDATE ---------------------------------------------
With this code Works:
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
"FireFoxPortable", "FireFox.exe");
FirefoxProfile profile = new FirefoxProfile();
var driver = new FirefoxDriver(new FirefoxBinary(path),profile);
But a Warning for new FirefoxDriver Shown: FirefoxDriver should not be constructed with a FirefoxBinary object. Use FirefoxOptions instead. This constructor will be removed in a future release.'
What's the correct way?

Selenium C# - Loading a profile throws exception in Firefox 48

I am using gecko/marionette driver and have tried both selenium 2 and selenium 3.
I've successfully started a Firefox session with the web driver and without a profile using both options and services to specify a binary:
FirefoxOptions options = new FirefoxOptions();
options.BrowserExecutableLocation = #"C:\Program Files (x86)\Mozilla\Firefox\firefox.exe";
driver = new FirefoxDriver(options);
or
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
driver = new FirefoxDriver(service);
However, when I try to load a profile into it by adding options:
options.Profile = profile;
it gives a corrupt deflate stream exceptionn.
This also occurs when I manually specify the binary file and then try to load the profile (which is deprecated in version 3)
This same profile grab and loading was working prior to the new Firefox update. Is there anything special that needs to be done to get this working?
I've encountered the same problem corrupt deflate stream when loading custom profile, and here is what helped me:
I opened the folder of my custom profile %AppData%\Mozilla\Firefox\Profiles\TestProfile and deleted all empty (0 kB size) files. (they were "AlternateServices.txt" and "parent.lock" in the root of profile directory)
After that the following code worked like a charm:
var FirefoxProfileManager = new FirefoxProfileManager();
var profile = FirefoxProfileManager.GetProfile("TestProfile");
//driver = new FirefoxDriver(profile);
var firefoxService = FirefoxDriverService.CreateDefaultService();
var options = new FirefoxOptions();
options.Profile = profile;
driver = new FirefoxDriver(firefoxService, options, new TimeSpan(0, 0, 30));
The below works for me when using the default profile, I have used this to solve an issue where-by its not using a root cert that we need to get through our security system:
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\\TestData\Dependencies", "geckodriver.exe");
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
FirefoxProfileManager profileM = new FirefoxProfileManager();
FirefoxProfile profile = profileM.GetProfile("default");
//service.Port = 64444;
FirefoxOptions options = new FirefoxOptions();
options.Profile = profile;
Instance = new FirefoxDriver(service,options, TimeSpan.FromMinutes(1));
TurnOnWait();
HTH

Categories