ChromeOptions and DesiredCapabilities relations in Selenium and C# - c#

I've been searching for the correct documentation on how to use ChromeOptions and DesiredCapabilities in the atmosphere of Selenium and C#, but since it's all open sourced, I only find suggestions (and they are not helping sometimes). My question today is how to setup the correct relation between ChromeOptions and DesiredCapabilities. Seems like I'm doing everything correctly, but still getting System.InvalidOperationException: unknown error:cannot parse capability: chromeOptions from unknown error: unrecognized chrome option:Arguments My code is following:
private static ChromeOptions Ops()
{
var options = new ChromeOptions();
options.AddArgument("--no-startup-window");
options.BinaryLocation = #"C:\path\path\path\chromedriver.exe";
return options;
}
private static DesiredCapabilities Caps()
{
DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability(CapabilityType.BrowserName, "chrome");
caps.SetCapability(ChromeOptions.Capability,Ops().ToCapabilities());
return caps;
}
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), Caps());
Can't find a place where incorrect Arguments are passing. Has anybody faced the same issues? This is ChromeDriver version 2.28 and selenium WebDriver v 3.3.0 Google Chrome browser version is 52.

You don't need to set the browser name; ChromeOptions does that for you.
According to this comment
The .NET bindings are moving toward a pattern where DesiredCapabilites should not be
used directly, even with RemoteWebDriver. To facilitate that, the ChromeOptions class
has a ToCapabilities() method
And there's this comment
Much like --disable-javascript, the chromedriver will not work if you use --no-startup-window.
It needs to launch a window to establish the connection with the AutomationProxy.
So that gets us to this:
var options = new ChromeOptions();
options.BinaryLocation = #"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), options.ToCapabilities());
However, are you actually running a grid? If you're testing on a single machine it's even simpler:
IWebDriver driver = new ChromeDriver();

Related

Disabling 'Save address' chrome browser popups using C# Selenium

Need help to block save address pop up coming in chrome browser while executing selenium c# automation scripts.
Following all options already tried but no luck.
IWebDriver driver;
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--disable-single-click-autofill");
chromeOptions.AddArgument("--disable-popup-blocking");
chromeOptions.AddExcludedArgument("--disable-infobars");
chromeOptions.AddAdditionalChromeOption("useAutomationExtension", "false");
chromeOptions.AddArgument("--disable-notifications");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());
driver = new ChromeDriver(chromeOptions);
I was able to turn off the Save Address prompt using AddUserProfilePreference. It is called "autofill.profile_enabled". You can just add it to your ChromeOptions and pass those options in when instantiating the driver.
I got the name from this Reddit post:
https://www.reddit.com/r/selenium/comments/yicwf0/chromium_how_to_disable_save_and_autofill_address/
ChromeOptions options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddArgument("test-type");
options.AddArgument("disable-notifications");
options.AddUserProfilePreference("autofill.profile_enabled", false);
IWebDriver webDriver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options, TimeSpan.FromSeconds(240));

chrome driver wont start session with given profile C#

So i've been trying to start my selenium session with my own profile and here is my code:
ChromeOptions options = new ChromeOptions();
options.AddArguments("user-data-dir=C:\\Users\\Mymsi\\AppData\\Local\\Google\\Chrome\\User Data");
options.AddArguments("--start-maximized");
IWebDriver driver = new ChromeDriver(#"C:\Users\aliza\Downloads");
driver = new ChromeDriver(options);
but for some reason selenium keeps starting a new session. Can someone tell me what am doing wrong?
Initiate driver as in below code
IWebDriver driver = new ChromeDriver(#"C:\Users\aliza\Downloads", options);

Linking selenium with electron framework (c#)

I've already written few lines of code in C# using Selenium webdriver. As my application was transferred to the Electron framework everything has changed and honestly, I don't know how to cope with it right now.
Could you please clarify it to me? What steps should I take to simple start... I would like to continue my work in the current project (selenium, C#), but I'm not sure that it's possible, or I should completely start from scratch using a different language and framework?
I've read about Spectron, and checked the internet resources like stackoverflow, however I'm still in the point of unawareness...
Spectron with mocha is supposed to be faster.
But still here is what you need.This is Java & Selenium.
System.setProperty("webdriver.chrome.driver","C:\\electron-chromedriver\\bin\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("C:\\Users\\app.exe");
chromeOptions.addArguments("start-maximized");
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability(CapabilityType.BROWSER_NAME, "Chrome");
capability.setCapability("chromeOptions", chromeOptions);
driver = new ChromeDriver(chromeOptions);
I have used the packaged electron app for binary (i.e) app.exe .
I think this is what you need.
Described below is related to using of Electron with .Net C# OpenQA.Selenium.
If you want to run electron app which being developed (consists of files index.html, main.js, etc.) you have to add following options (pay attention to 'app=' in cmd argument):
var options = new ChromeOptions();
options.BinaryLocation = #"your_path_to_electron\electron.exe";
options.AddArgument(#" app=path_to_folder_with_your_electron_app_src");
But if you want to run packaged electron app (*.exe) it is enough to use:
var options = new ChromeOptions();
options.BinaryLocation = #"path_to_folder_with_your_electron_app\your_electron_app.exe";
Also you can start any version of chromedriver.exe:
var service = ChromeDriverService.CreateDefaultService(path_to_folder_with_driver);
var driver = new ChromeDriver(service, options);
It might be helpful because I know that different electron applications are built on using of different version's drivers.
Try this for Electron application Initialization:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
namespace Selenium_Demo
{
class Selenium_Demo
{
IWebDriver driver;
[SetUp]
public void Start_Browser()
{
ChromeOptions options = new ChromeOptions();
ChromeDriverService chromeService = ChromeDriverService.CreateDefaultService(#"C:\\selenium\\chromedriver_win32v\\chromedriver.exe",
#"C:\\Program Files\\Cerebrata\\Cerebrata.exe");
driver = new ChromeDriver(chromeService, options);
}
[Test]
public void Test()
{
System.Threading.Thread.Sleep(6000);
Console.WriteLine("Test Passed");
}
[TearDown]
public void Close_Browser()
{
driver.Quit();
}
}
}

How to provide OperaWebDriver on c# use Selenium WebDriver

How to provide OperaWebDriver on c# use Selenium WebDriver
IWebDriver aDriver = new OperaDriver("path_to_operadriver.exe);
I have exception :
System.InvalidOperationException : unknown error: cannot find Opera
binary (Driver info: OperaDriver=0.2.0 )
Using setBinary gives me an error. I am able to resolve it by using this:
options.BinaryLocation = #"/path/to/opera";
Similarly for chrome as well.
You've got to download opera chromium driver.
Here's a snippet how it should work:
DesiredCapabilities capabilities = DesiredCapabilities.opera();
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/opera");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

how to use a FirefoxProfile with a IWebDriver

I have lots of problem with Firefox. For most of them, I found lots of solution that use FirefoxDriver object. However I need to use RemoteWebDriver.
All the solutions to my problem use a FirefoxProfile object. is there a way to use this profile for the IWebDriver?
One of the thing I need to do is use the profile to do this:
Firefox webdriver opens first run page all the time
here is how I use my IWebDriver:
driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), DesiredCapabilities.Firefox(), TimeSpan.FromSeconds(timeout));
First create the FirefoxProfile and then add it to the capabilities:
// create the profile
var profile = new FirefoxProfile();
// create the capabilities
var capabilities = DesiredCapabilities.Firefox();
capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());
// start the remote driver
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);

Categories