Selenium Chrome does not add extensions and arguments - c#

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'}

Related

Selenium C# - setting profile path

I want to use a specific firefox profile stored in a folder.
My code so far:
IWebDriver driver;
string pathToCurrentUserProfiles = #"C:\FirefoxProfile\";
string[] pathsToProfiles = Directory.GetDirectories(pathToCurrentUserProfiles, "*.default");
FirefoxProfile profile = new FirefoxProfile(pathsToProfiles[0]);
driver = new FirefoxDriver(profile);
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10);
driver.Navigate().GoToUrl("https://localhost");
Visual Studio though shows me this error:
Error CS1503 Argument 1: cannot convert from 'OpenQA.Selenium.Firefox.FirefoxProfile' to 'OpenQA.Selenium.Firefox.FirefoxOptions'
You need to create a FirefoxOptions object and set the Profile property:
FirefoxProfile profile = new FirefoxProfile(pathsToProfiles[0]);
FirefoxOptions options = new FirefoxOptions()
{
Profile = profile
};
driver = new FirefoxDriver(options);
You can infer this solution by looking at the arguments that the FirefoxDriver constructor accepts.

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

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

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!)

Disabling a popup

I am trying to Disable a popup which keeps showing up, which is a "Chrome Extension".
I am attaching a screenshot and also the code.
What I am trying to do is I want to click the Disable button.
The code I am using is:
public static void Initialize()
{
//Instance = new FirefoxDriver();
//Instance = new InternetExplorerDriver(#"C:\Sele2");
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-popup-blocking");
var basePath = AppDomain.CurrentDomain.BaseDirectory;
Instance = new ChromeDriver(basePath, options);
Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
}
Can someone please help? Thanks in advance
You can add one more arguments as --disable-extensions in your chrome options as below :-
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-popup-blocking","--disable-extensions");
var basePath = AppDomain.CurrentDomain.BaseDirectory;
Instance = new ChromeDriver(basePath, options);
Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));

Disable images in Selenium Google ChromeDriver

How does one disable images in Google chrome when using it through Selenium and c#?
I've attempted 6 ways and none worked. I've even tried the answer on this StackOverflow question, however I think the info in it is out of date.
Chrome driver: V2.2
Chrome version: V29.0.1547.66 m
Selenium: V2.35
All the attempts I've made don't cause exceptions, they run normally but still display images:
Attempt 1:
ChromeOptions co = new ChromeOptions();
co.AddArgument("--disable-images");
IWebDriver driver = new ChromeDriver(co);
Attempt 2:
DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability("chrome.switches", new string[1] { "disable-images" });
Attempt 3:
ChromeOptions co = new ChromeOptions();
co.AddAdditionalCapability("chrome.switches", new string[1] { "disable-images" });
Attempt 4:
var imageSetting = new Dictionary<string, object>();
imageSetting.Add("images", 2);
Dictionary<string, object> content = new Dictionary<string, object>();
content.Add("profile.default_content_settings", imageSetting);
var prefs = new Dictionary<string, object>();
prefs.Add("prefs", content);
var options = new ChromeOptions();
var field = options.GetType().GetField("additionalCapabilities", BindingFlags.Instance | BindingFlags.NonPublic);
if (field != null)
{
var dict = field.GetValue(options) as IDictionary<string, object>;
if (dict != null)
dict.Add(ChromeOptions.Capability, prefs);
}
Attempt 5:
ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("profile.default_content_settings", 2);
Attempt 6:
Dictionary<String, Object> contentSettings = new Dictionary<String, Object>();
contentSettings.Add("images", 2);
Dictionary<String, Object> preferences = new Dictionary<String, Object>();
preferences.Add("profile.default_content_settings", contentSettings);
DesiredCapabilities caps = DesiredCapabilities.Chrome();
caps.SetCapability("chrome.prefs", preferences);
This is my solution
IWebDriver driver;
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
driver = new ChromeDriver(options);
You should use --blink-settings instead of --disable-images by:
options.add_argument('--blink-settings=imagesEnabled=false')
which also works in headless mode. Setting profile doesn't work in headless mode. You can verify it by screenshot:
driver.get_screenshot_as_file('headless.png')
Note: I was using python with selenium, but I think it should be easy to transfer to c#.
For your method 1-3, I don't see a Chrome switch called --disable-images listed here. So even if the code snippets are correct, they won't work no matter what. Where did you get that switch? Any references?
For the methods 4-6, I assume you got the idea from this chromdriver issue. I don't know if this {'profile.default_content_settings': {'images': 2}} is still valid or not, but you can give it a try with the following code (which was originally the answer to How to set Chrome preferences using Selenium Webdriver .NET binding?, answer provided by Martin Devillers).
public class ChromeOptionsWithPrefs: ChromeOptions {
public Dictionary<string,object> prefs { get; set; }
}
public static void Initialize() {
var options = new ChromeOptionsWithPrefs();
options.prefs = new Dictionary<string, object> {
{ "profile.default_content_settings", new Dictionary<string, object>() { "images", 2 } }
};
var driver = new ChromeDriver(options);
}
Use http://chrome-extension-downloader.com/ to download the "Block Image" extension (https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj?hl=en-GB). The extension prevents the image from being downloaded in the first place. Now it's just a matter of loading it using the following statement:
var options = new ChromeOptions();
//use the block image extension to prevent images from downloading.
options.AddExtension("Block-image_v1.0.crx");
var driver = new ChromeDriver(options);
An easier approach would be solely:
ChromeOptions options = new ChromeOptions();
options.addArguments("headless","--blink-settings=imagesEnabled=false");
I found out simple solution. This idea referred from Python:Disable images in Selenium Google ChromeDriver
var service = ChromeDriverService.CreateDefaultService(#WebDriverPath);
var options = net ChromeOptions();
options.AddUserProfilePreference("profile.managed_default_content_settings.images", 2);
IWebDriver Driver = new ChromeDriver(service, options);
I recommend that you create a new profile, customize this profile so that it does not load the images, and use this profile as a chromeOption to set up your driver.
See: this article

Categories