Selenium 3.5 Options.ToCapabilities is not applying the options to RemoteWebDriver - c#

I had a couple of cmd line options working with Selenium 3.3 as follows:
`DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.Chrome();
options.AddArguments("--lang=en-GB");
options.AddArguments("--high-dpi-support=1");
options.AddArguments("--force-device-scale-factor=0.8");
capabilities = options.ToCapabilities() as DesiredCapabilities;
Driver = new RemoteWebDriver(new Uri("WIN10:5566/wd/hub"), capabilities,
TimeSpan.FromSeconds(180));`
However switching to Selenium 3.5.2, these options are no longer being applied even when using new ToCapabilities() as follows:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--lang=en-GB");
options.AddArguments("--high-dpi-support=1");
options.AddArguments("--force-device-scale-factor=0.5");
Driver = new RemoteWebDriver(new Uri("http://WIN10:5566/wd/hub"), options.ToCapabilities(), TimeSpan.FromSeconds(180));
Is there something else I need?

Try replacing
capabilities = options.ToCapabilities() as DesiredCapabilities;
with the following the following:
capability.setCapability(ChromeOptions.CAPABILITY, options);
Then you only need to specify "capability" in the RemoteWebDriver:
RemoteWebDriver(new URL("your url"), capability);

Thanks smit, that would have worked as well. The problem was the chrome driver needed updating (embarrassed emoji here!)

Related

Selenium Chrome does not add extensions and arguments

It looks like Selenium doesn't apply any options to the new browser instance, since neither the extension nor the arguments that were defined in code are applied, it just launches a regular Chrome window without extensions, with an infobar and no incognito mode
void ChromeSession()
{
const string siteUrl = "https://www.google.com/";
var options = new ChromeOptions();
options.AddArgument("--disable-infobars");
options.AddArgument("--incognito");
options.AddExtension(#"C:\Users\kiespetch1\Downloads\Solver.crx");
new DriverManager().SetUpDriver(new ChromeConfig());
var driver = new ChromeDriver();
driver.Url = siteUrl;
}
How I can add extensions and launch arguments?
You can add profiles
options.AddArgument = ("user-data-dir':'/Users/Application/Chrome/Default'}

Your connection is not secure - using Selenium.WebDriver v.3.6.0 + Firefox v.56

I'm writing tests with Selenium + C# and I face an important issue because I didn't found solution when I test my site with secure connection (HTTPS). All solutions I found on stackoverflow are out of date or doesn't work.
I tried to exercise all solutions from below question:
Selenium Why setting acceptuntrustedcertificates to true for firefox driver doesn't work?
But they did not help me solve the problem
Nor is it the solution of using Nightly FireFox.
Still, when the selenium loading Firfox browser, I see the page: "Your connection is not secure".
Configuration:
Firefox v56.0
Selenium.Firefox.WebDriver v0.19.0
Selenium.WebDriver v3.6.0
my code is:
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.AcceptUntrustedCertificates = true;
profile.AssumeUntrustedCertificateIssuer = false;
options.Profile = profile;
driver = new FirefoxDriver(FirefoxDriverService.CreateDefaultService() , options , TimeSpan.FromSeconds(5));
Drivers.Add(Browsers.Firefox.ToString() , driver);
Thank for your help!
Updates to my question here:
Note 1: To anyone who has marked my question as a duplicate of this question:
Firefox selenium webdriver gives “Insecure Connection”
I thought that it is same issue, but I need solution for C#, I try match your JAVA code to my above code
First, I changed to TRUE the below statment:
profile.AssumeUntrustedCertificateIssuer = true;
second, I create new FF profile ("AutomationTestsProfile")
and try to use it:
Try 1:
FirefoxProfile profile = new FirefoxProfileManager().GetProfile("AutomationTestsProfile");
try 2:
FirefoxProfile profile = new FirefoxProfile("AutomationTestsProfile");
I Run 2 options, but still the issue exists.
Note 2: I attached screenshot of my problem, it appears when the driver try to enter text to user-name on login page.
I noticed that when I open my site with FF, Firefox displays a lock icon with red strike-through red strikethrough icon in the address bar,
but near the username textbox not appears the msg:
"This connection is not secure. Logins entered here could be compromised. Learn More" (as you writed on the duplicate question),
So maybe there is a different problem?
You are setting the properties on the profile. The FirefoxOptions has a property AcceptInsecureCertificates, set that to true.
Forget the profile, this is what you want:
var op = new FirefoxOptions
{
AcceptInsecureCertificates = true
};
Instance = new FirefoxDriver(op);
For me, the profile setting AcceptUntrustedCertificates was not enough, I also had to set option security.cert_pinning.enforcement_level. My startup looks like
// no idea why FirefoxWebDriver needs this, but it will throw without
// https://stackoverflow.com/questions/56802715/firefoxwebdriver-no-data-is-available-for-encoding-437
CodePagesEncodingProvider.Instance.GetEncoding(437);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var service = FirefoxDriverService.CreateDefaultService(Environment.CurrentDirectory);
service.FirefoxBinaryPath = Config.GetConfigurationString("FirefoxBinaryPath"); // path in appsettings
var options = new FirefoxOptions();
options.SetPreference("security.cert_pinning.enforcement_level", 0);
options.SetPreference("security.enterprise_roots.enabled", true);
var profile = new FirefoxProfile()
{
AcceptUntrustedCertificates = true,
AssumeUntrustedCertificateIssuer = false,
};
options.Profile = profile;
var driver = new FirefoxDriver(service, options);
It works for me for following settings (same as above):
My env:
win 7
firefox 61.0.2 (64-bit)
Selenium C# webdriver : 3.14.0
geckodriver-v0.21.0-win32.zip
==============================
FirefoxOptions options = new FirefoxOptions();
options.BrowserExecutableLocation = #"C:\Program Files\Mozilla Firefox\firefox.exe";
options.AcceptInsecureCertificates = true;
new FirefoxDriver(RelativePath,options);

How do you set the port for ChromeDriver in Selenium?

As some background, I'm playing around with BrowserMob proxy and I am getting blocked by the port settings. So I'd like to know how to set a port by myself.
Server srv = new Server(#"C:\BMob\browsermob\bin\browsermob-proxy.bat");
srv.Start();
Client cln = srv.CreateProxy();
cln.NewHar("BOWZA");
ChromeOptions co = new ChromeOptions();
Proxy seleniumProxy = new Proxy { HttpProxy = cln.SeleniumProxy };
co.Proxy = seleniumProxy;
ChromeDriver cDriver = new ChromeDriver(co);
// What do I do now...?
I just failed to find anything documenting this, sorry
This should work:
ChromeDriverService service= ChromeDriverService.CreateDefaultService(DRIVER_PATH);
service.Port = <PORT>;
IWebDriver WebDriver = new ChromeDriver(service);
This works for me. I'm using webDriverManager as well
ChromeDriverService chromeDriverService = new ChromeDriverService.Builder().usingPort(DesiredPortNumber).build();
Then use it as a parameter when initializing new driver
new ChromeDriver(chromeDriverService , BrowserOptionsManager.getChromeOptions());

Unable to launch Chrome in incognito mode using Selenium WebDriver using C#

I am trying to launch Chrome using Selenium WebDriver in incognito mode, but not able to accomplish. I tried all options but not able to launch. Below is my code snippet
case "chrome":
ChromeOptions options = new ChromeOptions();
options.AddArgument("--incognito"); //Line XYZ
desiredCapabilities = DesiredCapabilities.Chrome();
desiredCapabilities.SetCapability(ChromeOptions.Capability, options);
break;
var capabilities = BuildDesiredCapabilities();
webDriver = new RemoteWebDriver(new Uri(gridHubURL), capabilities,
TimeSpan.FromSeconds(ApplicationConfiguration.RemoteDriverTimeOutValue));
Can anyone please help me what I am doing wrong here? I also tried the below code options in Line XYZ
Any pointers will be much helpful.
EDIT1
Please find the updated code here.
public IWebDriver CreateDriver()
{
var capabilities = BuildDesiredCapabilities();
webDriver = new RemoteWebDriver(new Uri(gridHubURL), capabilities,
TimeSpan.FromSeconds(ApplicationConfiguration.RemoteDriverTimeOutValue));
webDriver.Manage().Window.Maximize();
webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(ApplicationConfiguration.TimeOutValue));
webDriver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(ApplicationConfiguration.TimeOutValue));
return webDriver;
}
private DesiredCapabilities BuildDesiredCapabilities()
{
DesiredCapabilities desiredCapabilities;
switch (browserName.ToLower())
{
case "firefox":
desiredCapabilities = DesiredCapabilities.Firefox();
break;
case "chrome":
desiredCapabilities = DesiredCapabilities.Chrome();
desiredCapabilities.SetCapability("chrome.switches", "--incognito");
break;
case "ie":
desiredCapabilities = DesiredCapabilities.InternetExplorer();
desiredCapabilities.SetCapability("ie.ensureCleanSession", true);
break;
default:
desiredCapabilities = DesiredCapabilities.Firefox();
break;
}
return desiredCapabilities;
}
The .NET bindings have introduced browser-specific Options classes to avoid having to know or understand arbitrary capability values. You were using just such a class, ChromeOptions, in your original code. You missed one extra step, though, in how to use the ChromeOptions class with RemoteWebDriver. The missing piece is that you should use the ToCapabilities() method to convert the ChromeOptions object to the ICapabilities object that RemoteWebDriver expects. Your code would look something like the following:
var options = new ChromeOptions();
options.AddArgument("incognito");
var capabilities = options.ToCapabilities();
var driver = new RemoteWebDriver(new URI(gridHubURL), capabilities);
You should pass parameters to the executable like this:
desiredCapabilities = DesiredCapabilities.Chrome();
desiredCapabilities.SetCapability("chrome.switches", "--incognito");
So passing the parameter --incognito to the chrome.switches capability should work.
NOTE:
The chrome.switches capability has been deprecated for over two years. A list of the current supported capabilities can be found at the official chromedriver Google Sites page. Additionally, use of arbitrary capabilities has been discouraged by the Selenium project for some time, particularly when using the .NET bindings

selenium change firefox proxy to autoDetect c#

I search to change the default proxy of firefox on firefoxDriver call.
I work in c#.
I know in java you do :
FirefoxProfile ff = new FirefoxProfile();
ff.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal());
FirefoxDriver ffD = new FirefoxDriver(ff);
I tried :
FirefoxProfile ff = new FirefoxProfile();
ff.SetPreference ("network.proxy.type",ProxyKind.AutoDetect.ToString ());
var driver = new FirefoxDriver(ff);
I just forgot whats ordinal. After that.
ff.SetPreference ("network.proxy.type",(int)ProxyKind.AutoDetect);

Categories