Not able to accept certificates in Selenium 3.7 C#, Firefox 57 - c#

I have upgraded Selenium and Firefox to the latest but the website I am testing now lands on the "Your connection is not secure" page and I can't get any of the suggestions online to work such as...
FirefoxOpts.SetPreference("webdriver_assume_untrusted_issuer", true);
FirefoxOpts.SetPreference("webdriver_accept_untrusted_certs", true);
FirefoxOpts.AddAdditionalCapability("acceptSslCerts", true);
FirefoxOpts.AddAdditionalCapability("acceptInsecureCerts", true);
I have also tried creating a profile and using...
FirefoxProfile profile = profileManager.GetProfile("Selenium");
profile.SetPreference("webdriver.firefox.profile", "Selenium");
...but these don't work either. This is using Selenium Grid.
UPDATE
Code block for webdriver initiation is:
var capabilities = new DesiredCapabilities();
var FirefoxOpts = new FirefoxOptions();
var profileManager = new FirefoxProfileManager();
var profile = profileManager.GetProfile("Selenium");
//profile.SetPreference("webdriver.firefox.profile", "Selenium");
//profile.AcceptUntrustedCertificates = true;
//profile.AssumeUntrustedCertificateIssuer = true;
//profile.AcceptUntrustedCertificates = true;
//profile.AssumeUntrustedCertificateIssuer = true;
//capabilities.SetCapability(CapabilityType.AcceptSslCertificates, true);
//FirefoxOpts.AddAdditionalCapability(CapabilityType.AcceptSslCertificates, true);
FirefoxOpts.BrowserExecutableLocation = #"C:\Program Files\Mozilla Firefox\firefox.exe";
FirefoxOpts.SetPreference("intl.accept_languages", "en-GB");
FirefoxOpts.SetPreference("layout.css.devPixelsPerPx", "0.8");
FirefoxOpts.Profile = profile;
FirefoxOpts.ToCapabilities();
//FirefoxOpts.SetPreference("webdriver_assume_untrusted_issuer", true);
//FirefoxOpts.SetPreference("webdriver_accept_untrusted_certs", true);
//FirefoxOpts.AddAdditionalCapability("acceptSslCerts", true);
//FirefoxOpts.AddAdditionalCapability("acceptInsecureCerts", true);
//FirefoxOpts.AddAdditionalCapability(CapabilityType.AcceptInsecureCertificates, true);
Driver = new RemoteWebDriver(new Uri("http://" + Config.VM + ":5566/wd/hub"), FirefoxOpts);
There's lots I've commented out which I've tried previously but nothing works regarding accepting the certs or launching Firefox with a specified profile

This is most likely caused by your self signed dev certificate. I started having the same issues with chromedriver. The easiest fix was to add the certificate to the trusted root certificates.
run MMC
File->Add Snap-in
Click certificates and add
Go to local computer -> personal -> certificates
Locate and highlight your certificate, right click it and copy
Paste it into the Trusted Root Authority folder.
Firefox should be happy now.

Related

How to start driver with path, service and options

I work with driverĀ“s for Chrome, Firefox and Edge in my Application and have always the same problem.
So for this post i reduce my request to the Chrome driver as an example.
What is needed:
Path to the chromedriver.exe
Hide the console
Change of the default Download directory of the browser
Here is a Page that show us the Chrome Driver Class and its options:
https://www.selenium.dev/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Chrome_ChromeDriver.htm
But my 3 needed Points are not listed together.
Here is a snipped of my Code with Commends:
string str_DriverPath = #"C:\_MT5_TOOLS\DRIVER\CHROME";
// hide Console
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
//// change Standard-Download-Path
ChromeOptions options = new ChromeOptions();
var downloadDirectory = GlobalVars.RootPath + #"Pool\" + GlobalVars.strSymbol + #"\" + GlobalVars.strSymbol + #"_" + GlobalVars.strPeriod;
options.AddUserProfilePreference("download.default_directory", downloadDirectory);
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("disable-popup-blocking", "true");
// Start Driver:
//webdriver = new ChromeDriver(service, options); // works fine
//webdriver = new ChromeDriver(str_DriverPath, options); // works fine
webdriver = new ChromeDriver(str_DriverPath, service, options); // will not work
How to combine my 3 Points into one driver?
After trying every possible thing around the environment variable "PATH" to set the driver path, it seems to be impossible in 2022.
From manually until coded this still not work for me!
Maybe this was a trick around in the past...
Solution:
On another portal I found a really nice and easy working solution!
The second Line in the following Code does the Trick.
It is just the service, where the PATH to the driver can be placed in.
This works identical for Edge, Chrome, and Firefox.
// DriverService with Path to driver.exe
ChromeDriverService service = ChromeDriverService.CreateDefaultService(#"C:\_MT5_TOOLS\DRIVER\CHROME");
// hide driver Console? true/false
service.HideCommandPromptWindow = true;
// change Standard-Download-Path
ChromeOptions options = new ChromeOptions();
var downloadDirectory = GlobalVars.RootPath + #"Pool\" + GlobalVars.strSymbol + #"\" + GlobalVars.strSymbol + #"_" + GlobalVars.strPeriod;
options.AddUserProfilePreference("download.default_directory", downloadDirectory);
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("disable-popup-blocking", "true");
// Selenium Driver starten:
webdriver = new ChromeDriver(service, options);

Crawlera/Zyte proxy authentication using C# and Selenium

I've tried a number of ways of using Zyte (formally Crawerla) proxies with Selenium.
They provide
1- API key (username)
2- Proxy url/port.
No password is needed.
What I have tried...
ChromeOptions options = new ChromeOptions();
var proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.SocksUserName = "<<API KEY>>";
proxy.SocksPassword = "";
proxy.HttpProxy =
proxy.SslProxy = "proxy.crawlera.com:8011";
options.Proxy = proxy;
IWebDriver driver = new ChromeDriver(options);
Which, when selenium loads produces this:
Funnily enough, if I manually add the username (API Key) it will indeed load, but this defeats the purpose of automation.
The second method I tried was:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--proxy-server=<API KEY>::proxy.crawlera.com:8011");
options.AddArguments("--log-level=OFF");
IWebDriver driver = new ChromeDriver(options);
I used :: as the password is blank.
And the error to this method is:
[46784:44492:0219/015119.757:ERROR:validation_errors.cc(87)] Invalid
message: VALIDATION_ERROR_DESERIALIZATION_FAILED
I guess Zyte/Crawerla knowledge isn't really needed, it's more how to provide selenium with a username, but no password, and have it use the proxy successfully.
Does anybody have any idea? (examples appreciated)
you can use this Crawlera-Headless-Proxy https://github.com/zytedata/zyte-smartproxy-headless-proxy when using a headless browser.
This will act as a MITM proxy and will handle the Authentication of SPM as well.
So you will not need to mention the Crawlera/SmartProxyManager Authentication within your C# Selenium Script.
Crawlera-headless-proxy can be used as a standalone binary like this
crawlera-headless-proxy -c config.toml
You can mention the API Key, port number, and other configs in the config.toml file.
It will start the MITM server at the localhost:3128. In your C# you need to mention the proxy host/port should be localhost/127.0.0.1 and port as 3128.
The requests from your C# headless script needs to be sent to Crawlera-Headless-Proxy and then Crawlera-Headless-Proxy will send the request to the SmartProxyManager/Crawlera.
So your code may look like this for ex:
ChromeOptions options = new ChromeOptions();
var proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.HttpProxy =
proxy.SslProxy = "127.0.0.1:3128";
options.Proxy = proxy;
IWebDriver driver = new ChromeDriver(options);

Unable to instantiate Marionette driver with both a FirefoxProfile & FirefoxDriverService

I am currently updating my C# solution with the new Marionette driver for Firefox.
I have managed to get the driver to successfully launch a url with the following code
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(Directory.GetCurrentDirectory(),"wires-0.6.0-win.exe");
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
FirefoxDriver driver = new FirefoxDriver(service);
driver.Navigate().GoToUrl("http://www.google.com");
However i need to add a profile to the driver at initialisation in order to set the proxy. I was previously doing this like the following (for older Firefox versions).
private static IWebDriver InitialiseFirefoxDriver(Proxy proxy)
{
FirefoxProfile profile = new FirefoxProfile();
if (proxy != null)
{
profile.SetProxyPreferences(proxy);
}
return new FirefoxDriver(profile);
}
Unfortunately the FirefoxDriver constructor only allows me to pass in either a FirefoxDriverService or a FirefoxProfile. Does anybody know of a way that i can manage to give the driver both sets of config information before creating the driver (or even after)?
Thanks.
This is example code use FirefoxProfile & FirefoxDriverService. I want hide CommandPromptWindow and Mute.
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
FirefoxProfile _Profile = new FirefoxProfile();
_Profile.SetPreference("media.volume_scale", "0.0");
FirefoxOptions option = new FirefoxOptions();
option.Profile = _Profile;
var driver = new FirefoxDriver(service,option,TimeSpan.FromSeconds(20));
driver.Navigate().GoToUrl("https://www.youtube.com/watch?v=hxiitzCKRek");

Setting a proxy for Chrome Driver in Selenium

I am using Selenium Webdriver using C# for Automation in Chrome browser.
I need to check if my webpage is blocked in Some regions(some IP ranges). So I have to set a proxy in my Chrome browser. I tried the below code. The proxy is being set but I get an error. Could someone help me?
ChromeOptions options = new ChromeOptions();
options.AddArguments("--proxy-server=XXX.XXX.XXX.XXX");
IWebDriver Driver = new ChromeDriver(options);
Driver.Navigate().GoToUrl("myUrlGoesHere");
When I run this code, I get the following message in my Chrome browser: I tried to enable the Proxy option, but the ' Change proxy settings' option is disabled.
Unable to connect to the proxy server
A proxy server is a server that acts as an intermediary between your computer and other servers. Your system is currently configured to use a proxy, but Google Chrome can't connect to it.
If you use a proxy server...
Check your proxy settings or contact your network administrator to ensure the proxy server is working. If you don't believe you should be using a proxy server: Go to the Chrome menu > Settings > Show advanced settings... > Change proxy settings... > LAN Settings and deselect
"Use a proxy server for your LAN".
Error code: ERR_PROXY_CONNECTION_FAILED*
I'm using the nuget packages for Selenium 2.50.1 with this:
ChromeOptions options = new ChromeOptions();
proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.HttpProxy =
proxy.SslProxy = "127.0.0.1:3330";
options.Proxy = proxy;
options.AddArgument("ignore-certificate-errors");
var chromedriver = new ChromeDriver(options);
If your proxy requires user log in, you can set the proxy with login user/password details as below:
options.AddArguments("--proxy-server=http://user:password#yourProxyServer.com:8080");
Please Following code, this will help you to change the proxy
First create chrome extension and paste the following java script
code.
Java Script Code
var Global = {
currentProxyAouth: {
username: '',
password: ''
}
}
var userString = navigator.userAgent.split('$PC$');
if (userString.length > 1) {
var credential = userString[1];
var userInfo = credential.split(':');
if (userInfo.length > 1) {
Global.currentProxyAouth = {
username: userInfo[0],
password: userInfo[1]
}
}
}
chrome.webRequest.onAuthRequired.addListener(
function(details, callbackFn) {
console.log('onAuthRequired >>>: ', details, callbackFn);
callbackFn({
authCredentials: Global.currentProxyAouth
});
}, {
urls: ["<all_urls>"]
}, ["asyncBlocking"]);
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log('Background recieved a message: ', request);
POPUP_PARAMS = {};
if (request.command && requestHandler[request.command])
requestHandler[request.command](request);
}
);
C# Code
var cService = ChromeDriverService.CreateDefaultService();
cService.HideCommandPromptWindow = true;
var options = new ChromeOptions();
options.AddArguments("--proxy-server=" + "<< IP Address >>" + ":" + "<< Port Number >>");
options.AddExtension(#"C:\My Folder\ProxyChanger.crx");
options.Proxy = null;
string userAgent = "<< User Agent Text >>";
options.AddArgument($"--user-agent={userAgent}$PC${"<< User Name >>" + ":" + "<< Password >>"}");
IWebDriver _webDriver = new ChromeDriver(cService, options);
_webDriver.Navigate().GoToUrl("https://whatismyipaddress.com/");

Selenium Firefox Profile Path not taken into account

I just want to test locally. With Internet Explorer it works. With Firefox, I get a timeout on line driver.FindElement :
var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl(url);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement category = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Name("login"));
});
// Login
driver.FindElement(By.Name("login")).SendKeys("test");
Error message is httpRequest to remotedriver timeout.
Upate: I think it's due to the fact that I have a portable version of Firefox 21 and an older version of FF which cannot work with Selenium whereas Selenium launch old version. So I tried to indicate the path of portable :
var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
string path = #"C:\Portable";
FirefoxProfile ffprofile = new FirefoxProfile(path);
IWebDriver driver = new FirefoxDriver(ffprofile);
Unfortunately it keeps running the old version (I cannot change the old version because of corporate environment).
Is there anyway to make this profile work ?
Not sure if it is your problem, but in order to 'point' Selenium to where Firefox is located, you are looking for the FirefoxBinary class:
var binary = new FirefoxBinary("pathtofirefox");
string path = #"C:\Portable";
FirefoxProfile ffprofile = new FirefoxProfile(path);
var driver = new FirefoxDriver(binary, profile);

Categories

Resources