FireFox & Selenium not working on specific version - c#

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

Related

C# Selenium - Failed to Start Tor

I'm trying to launch Tor browser through Selenium in C# using the following code:
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AutomatorApp
{
public class BrowserAutomator
{
public void Automate()
{
String torPath = "D:\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "D:\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default\\";
FirefoxProfile profile = new FirefoxProfile(profilePath);
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.socks", "127.0.0.1");
profile.SetPreference("network.proxy.socks_port", 9153);
profile.SetPreference("network.proxy.socks_remote_dns", false);
FirefoxDriverService firefoxDriverService = FirefoxDriverService.CreateDefaultService("D:\\geckodriver-v0.26.0-win64", "geckodriver.exe");
firefoxDriverService.FirefoxBinaryPath = torPath;
var firefoxOptions = new FirefoxOptions
{
Profile = profile,
LogLevel = FirefoxDriverLogLevel.Trace
};
FirefoxDriver driver = new FirefoxDriver(firefoxDriverService, firefoxOptions);
}
}
}
However, this shows the error 'Tor Failed to Start' and the exception simply contains 'Permission denied'. I tried launching the app in administrator mode and ensuring that the folder is accessible by all users but this did not solve the issue.
Interestingly, when I try to launch a Firefox browser using the same setup it works well.
Any help is very much appreciated.
Update - Solved: After updating to the latest Tor version (9.5.1) The final working code:
FirefoxProfile profile = new FirefoxProfile(profilePath);
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.socks", "127.0.0.1");
profile.SetPreference("network.proxy.socks_port", 9153);
profile.SetPreference("network.proxy.socks_remote_dns", false);
FirefoxDriverService firefoxDriverService = FirefoxDriverService.CreateDefaultService(geckoDriverDirectory);
firefoxDriverService.FirefoxBinaryPath = torPath;
firefoxDriverService.BrowserCommunicationPort = 2828;
var firefoxOptions = new FirefoxOptions
{
Profile = null,
LogLevel = FirefoxDriverLogLevel.Trace
};
firefoxOptions.AddArguments("-profile", profilePath);
FirefoxDriver driver = new FirefoxDriver(firefoxDriverService, firefoxOptions);
driver.Navigate().GoToUrl("https://www.google.com");
Important notes:
The following TOR configs need to be changed in about:config :
marionette.enabled: true
marionette.port: set to an unused port, and set this value to firefoxDriverService.BrowserCommunicationPort in your code. This was set to 2828 in my example.
As far as I remember from my attempts a few years ago, TOR with WebDriver didn't work when you set that "Profile" option. Normal Firefox works, but TOR simply doesn't.
If you get a timeout error after this, make sure marionette is enabled on about:config. If it's already enabled, follow what is going on with TOR on start up. Like if the actual firefox browser doesn't load up, stuck at connection launhcer etc, or there is a message box at the browser startup or something... And also make sure no other firefox.exe, geckodriver.exe or tor.exe is running on the background. If multiple executubles are not configured to listen different port numbers, it might cause problems.
Environment.SetEnvironmentVariable("webdriver.gecko.driver", "C:\\TorBrowser\\Browser\\geckodriver.exe");
var gekcoService = FirefoxDriverService.CreateDefaultService("C:\\TorBrowser\\Browser", "geckodriver.exe");
var gekcoService.FirefoxBinaryPath = "C:\\TorBrowser\\Browser\\firefox.exe";
// marionette port that browser listens to. I had it set to a custom port on about:config
var gekcoService.BrowserCommunicationPort = 50111;
// also had given a custom port for geckodriver listen port
var gekcoService.Port = 9881;
var gekcoService.Host = 127.0.0.1;
var gekcoService.HostName = 127.0.0.1;
var gekcoService.Start();
var ffOptions = new FirefoxOptions
{
AcceptInsecureCertificates = true,
BrowserExecutableLocation = "C:\\TorBrowser\\Browser\\firefox.exe",
Profile = null,
UnhandledPromptBehavior = UnhandledPromptBehavior.Dismiss
};
ffOptions.AddArguments("-profile", "C:\\TorBrowser\\Browser\\TorBrowser\\Data\\Browser\\profile.default");
ffOptions.LogLevel = FirefoxDriverLogLevel.Info;
ffOptions.PageLoadStrategy = PageLoadStrategy.Eager;
var ffDriver = new FirefoxDriver(gekcoService, ffOptions);
ffDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10);
ffDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(10);
ffDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
Your code block looks perfect to me.
However there seems to be an issue opening the tor Browser 9.5 which uses the default Firefox v68.9.0esr.
You can find a detailed discussion in How to initiate a Tor Browser 9.5 which uses the default Firefox to 68.9.0esr using GeckoDriver and Selenium through Python
Solution
The solution will be to install and use either of the following browsers:
Firefox v77.0.1
Firefox Nightly v79.0a1
In case you use the above mentioned browsers, you need to:
Firefox v77.0.1:
String torPath = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
Firefox Nightly v79.0a1
String torPath = "C:\\Program Files\\Firefox Nightly\\firefox.exe";

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 cannot load specific created profile in firefox driver

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

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