Can't open Firefox-based Browser - c#

I tried to open modified FireFox browser using Selenium WebDriver.
(This Firefox-based browser as same as original Firefox, just with additional functionality.)
This Browser opens and then I got Error:
["OpenQA.Selenium.WebDriverException" in WebDriver.dll]
TypeError: Given browserName [object String] "firefox", but my name is [object String] "anotherbrowser"
My code[C#]:
var path = new FirefoxBinary(#"C:\FireFox_BasedBrowser\anotherbrowser.exe");
IWebDriver driver = new FirefoxDriver(path, null); //here's error
After searching I found this advice on Java:
String bname = "Browser name";
FirefoxOptions options = new FirefoxOptions();
options.setBinary("Path to browser binary");
options.setCapability("browserName", bname);
options.setCapability("marionette", false);
driver = new FirefoxDriver(options);
I tried to rewrite it into C#:
DesiredCapabilities cap = DesiredCapabilities.Firefox();
cap.SetCapability("browserName", "anotherbrowser");
cap.SetCapability("firefox_binary", #"C:\FireFox_BasedBrowser\anotherbrowser.exe");
IWebDriver driver = new FirefoxDriver(cap); //here's error
But I also got an error:
["System.ArgumentException" in WebDriver.dll] There is already an option for the browserName capability. Please use
the instead.
Please, help me, I can't find any solutions.
P.s. I can't use original Firefox browser, because it doesn't have same advantages as this modified Firefox.

Use FirefoxDriverService.CreateDefaultService(...) to define another path for Firefox:
var service = FirefoxDriverService.CreateDefaultService(#"C:\drivers", "geckodriver.exe");
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
var driver = new FirefoxDriver(service);

Related

Downloading Files with Headless Chrome - Selenium C#

ChromeOptions options = new ChromeOptions();
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
chromeDriverService.SuppressInitialDiagnosticInformation = true;
options.AddArgument("--headless");
string downloadPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Downloads";
options.AddUserProfilePreference("download.default_directory", downloadPath);
options.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 1);
options.AddArgument("--window-size=1920,1080");
For some reason I cannot download files in chrome when running headless in Selenium -
When not running in headless mode there is no issue downloading files.
Selenium Webdriver Chromedriver V110.0.5
According to Selenium dev page: https://www.selenium.dev/blog/2023/headless-is-going-away/
you need to user "--headless=new" from now.
However, I'm still struggling with downloading files, even using this new attribute
My use case was a bit different. However, the below code gave me the intended results.
Refer to the below code :
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", #"C:\Downloads");
options.AddUserProfilePreference("download.prompt_for_download", false);
ChromeDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl("https://example.com/download-file");
IWebElement downloadLink = driver.FindElement(By.Id("download-link"));
downloadLink.Click();
System.Threading.Thread.Sleep(4000); // wait for awhile for 4 seconds
driver.Close();

Using Extensions in firefox selenium

I am trying to use an extension in selenium for geckdriver and have found that I need to use FirefoxProfile. From here though, I am lost on what exactly to do. I can't find much help online. Here is what I have, but I get a System.TypeInitializationException error.
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
string workingDirectory1 = Environment.CurrentDirectory;
profile.AddExtension(workingDirectory1 + "\\anticaptcha.xpi");
options.Profile = profile;
IWebDriver driver = new FirefoxDriver(options);
driver.Navigate().GoToUrl("https://www.google.com/");
Looks like addExtension method needs some corrections it accepts File as a argument and not a string

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;

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