I see a ICapabilities interface to get the Browser info;Did couple of googling with no luck for any code example; Can anybody please share anything how I can get the browser info for a particular IWebDriver instance ? I am using C# webdriver.
In order to get info defined in ICapabilities interface, you need to cast IWebDriver instance to RemoteWebDriver. Then you can get the info about BrowserName, IsJavaScriptEnabled, Platform and Version.
IWebDriver driver = new FirefoxDriver();
ICapabilities capabilities = ((RemoteWebDriver)driver).Capabilities;
// then you have
// capabilities.BrowserName;
// capabilities.IsJavaScriptEnabled;
// capabilities.Platform;
// capabilities.Version;
Based on the old Yi Zeng answer, I was able to acces with the next code:
IWebDriver driver = new FirefoxDriver();
ICapabilities capabilities = ((WebDriver)driver).Capabilities;
// then you have
// capabilities.GetCapability("browserName");
// ...
I use below code to get Chrome driver version
IWebDriver driver = new ChromeDriver();
ICapabilities capabilities = ((OpenQA.Selenium.WebDriver)driver).Capabilities;
var SeleniumWebDriverName = driver.GetType().ToString();
var SeleniumWebDriverVersion = (capabilities.GetCapability("chrome") as Dictionary<string, object>)["chromedriverVersion"];
Console.WriteLine( "DRIVER NAME ====" + SeleniumWebDriverName );
Console.WriteLine( "VERSION ====" + SeleniumWebDriverVersion + Environment.NewLine);
I've stumbled across an easier way if you just need to know which driver is running to get around a hack:
Driver.GetType().ToString();
Related
(Previous thread)
(Thread for the same problem)
I want to extract information from a website, but in order to do that I need to login. Basically I can't authenticate with a HTTP/Web/Server request so I have to do it via browser, using WebDriver Selenium (Chrome). Now since the information can't be found through "view source-code", driver.PageSource doesn't work for example. Now when I do what has been suggested in the mentioned thread, I still get the wrong source code (there has also been the same problem reported in the comments). I need to get the code from the Inspector.
Any ideas? Thanks in advance!
class Program
{
static void Main(string[] args)
{
string url = "link";
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl(url);
IWebElement email = driver.FindElement(By.Id("login-email-input-field"));
IWebElement password = driver.FindElement(By.Id("login-password-input"));
email.SendKeys("email");
password.SendKeys("password");
IWebElement login = driver.FindElement(By.Id("login-btn"));
login.Click();
driver.Navigate().GoToUrl(url);
Thread.Sleep(10000);
//Trying to get the inspect element code here
IWebElement element = driver.FindElement(By.Id("id"));
var html = element.GetAttribute("innerHTML");
Console.WriteLine(html);
}
}
}
I'm trying to use Selenium RemoteWebDriver (in C#) with Multiloginapp.
Here is my code for initiating a driver:
int mlaClientPort = 35000;
Uri driverUrl = new Uri("http://127.0.0.1:" + mlaClientPort + "/api/v1/webdriver");
// set desired capabilities to connect to a pre-defined browser profile
DesiredCapabilities dc = new DesiredCapabilities();
dc.SetCapability("multiloginapp-profileId", profileId);
RemoteWebDriver driver = new RemoteWebDriver(driverUrl, dc);
It starts up the browser fine (FireFox).
However, when I attempt to execute Javascript like below:
var jsEx = (IJavaScriptExecutor)driver;
object result = jsEx.ExecuteScript("alert('test');");
I get the error:
'Underlying driver instance does not support executing javascript
This is my first time using RemoteWebDriver but as far as I see, javascript is working in the browser when using it from the console etc.
What could be the issue here?
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);
When I try to use
var dc = DesiredCapabilities.Chrome();
var driver = new ChromeDriver(dc);
I get "Cannot resolve constructor".
It seems like I have to pass ChromeOptions instead.
Why?
Every single tutorial/help page on the subject suggests that I pass DesiredCapabilities.
I am using Selenium.WebDriver.ChromeDriver version 2.21.0.0.
You can use ChromeOptions to set any specific options.
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-extensions");
options.AddArguments("--start-maximized");
options.ToCapabilities();
ChromeDriverService service = ChromeDriverService.CreateDefaultService(Environment.GetEnvironmentVariable("USERPROFILE") + "\\Downloads");
IWebDriver chromeDriver = new ChromeDriver(service, options);
You can use- options.ToCapabilities(); to get see the capabilities.
You can use ChromeOptions to set any specific type of capabilities- peter.sh/experiments/chromium-command-line-switches . It seems the DesiredCapabilities can be added only in Java or if you are dealing with InternetExplorerDriver- Selenium c#: How to launch Internet Explorer driver in a specific version (IE8 for example)
Using dotpeek and looking at the chromedriver constructors (which there are 7 overloads) 6 of them invoke the constructor below on the ChromeDriver itself
public ChromeDriver(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
: base((ICommandExecutor) new DriverServiceCommandExecutor((DriverService) service, commandTimeout), ChromeDriver.ConvertOptionsToCapabilities(options))
{
}
Which in turn calls the base constructor on the RemoteWebdriver. This passes in the last parameter as ChromeDriver.ConvertOptionsToCapabilities(options)
Looking at the you can see this:
private static ICapabilities ConvertOptionsToCapabilities(ChromeOptions options)
{
if (options == null)
throw new ArgumentNullException("options", "options must not be null");
return options.ToCapabilities();
}
Then into options.ToCapabilities:
public override ICapabilities ToCapabilities()
{
Dictionary<string, object> dictionary = this.BuildChromeOptionsDictionary();
DesiredCapabilities desiredCapabilities = DesiredCapabilities.Chrome();
desiredCapabilities.SetCapability(ChromeOptions.Capability, (object) dictionary);
if (this.proxy != null)
desiredCapabilities.SetCapability(CapabilityType.Proxy, (object) this.proxy);
Dictionary<string, object> preferencesDictionary = this.GenerateLoggingPreferencesDictionary();
if (preferencesDictionary != null)
desiredCapabilities.SetCapability(CapabilityType.LoggingPreferences, (object) preferencesDictionary);
foreach (KeyValuePair<string, object> additionalCapability in this.additionalCapabilities)
desiredCapabilities.SetCapability(additionalCapability.Key, additionalCapability.Value);
You can see under the hood it appears its already using DesiredCapabilities.Chrome() and you don't need to pass it in. Perhaps the tutorials you have seen are outdated?
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