Reputation: 31
I am trying to use WebDriverManager Package for .Net for automatically managing the chromedriver.exe version while executing a test case. But when ever I try to type cast ChromeDriver object to RemoteDriverObject it is showing me below error.
Unable to cast object of type 'OpenQA.Selenium.Chrome.ChromeDriver' to type 'OpenQA.Selenium.Remote.RemoteWebDriver'.
I will write the methods and the code that I am using below:
public IWebDriver WebInit()
{
ChromeOptions options = new ChromeOptions();
options.AddArguments("--test-type");
options.AddArguments("--start-maximized");
options.AddArguments("--disable-infobars");
options.AddArguments("--disable-extensions");
options.AddAdditionalCapability("useAutomationExtension", false);
options.AddArguments("--ignore-certificate-errors");
options.AddExcludedArgument("enable-automation");
options.AddUserProfilePreference("credentials_enable_service", false);
options.AddUserProfilePreference("profile.password_manager_enabled", false);
options.AddAdditionalCapability("useAutomationExtension", false);
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("download.default_directory", downloadFilepath);
options.AddUserProfilePreference("safebrowsing.enabled", true);
options.AddArguments("window-size=1600,900");
new DriverManager().SetUpDriver(new ChromeConfig(), VersionResolveStrategy.MatchingBrowser); //this is the code used from the mentioned Github Repository
_driver = new ChromeDriver(options);
}
public void FunctionWhereIAmCallingTheWebInitFucntion()
{
_driver = WebInit()
ICapabilities capabilities = ((RemoteWebDriver)_driver).Capabilities; //whenever this line gets executed it throws the exception that is mentioned
}
Below are the Package versions that I am using
<PackageReference Include="Selenium.Support" Version="4.1.1" />
<PackageReference Include="Selenium.WebDriver" Version="4.1.1" />
<PackageReference Include="WebDriverManager" Version="2.12.4" />
Please can anyone from the community can guide me where am I making a mistake? Thank you very much!!
Upvotes: 1
Views: 3863
Reputation: 76
Alternately, without forcing method signatures or references to concrete types like ChromeDriver
or similar, you can use the typing of the base WebDriver
type to safely cast to IHasCapabilities
to reference the ICapabilities
object.
See source - WebDriver.cs
public class WebDriver :
IWebDriver,
ISearchContext,
IDisposable,
IJavaScriptExecutor,
IFindsElement,
ITakesScreenshot,
ISupportsPrint,
IActionExecutor,
IAllowsFileDetection,
IHasCapabilities,
...
Given your example code -
public IWebDriver WebInit() { /* snipped */ }
public void FunctionWhereIAmCallingTheWebInitFunction()
{
_driver = WebInit();
//whenever this line gets executed it throws the exception that is mentioned
ICapabilities capabilities = ((RemoteWebDriver)_driver).Capabilities;
}
We can make a small change to reference the property (but we know by inheritance that it's going to implement IHasCapabilities
(as of Selenium.WebDriver 4.23.0
), so we could direct-cast effectively.
public IWebDriver WebInit() { /* snipped */ }
public void FunctionWhereIAmCallingTheWebInitFunction()
{
_driver = WebInit();
if (_driver is IHasCapabilities obj)
{
var browserName = obj.Capabilities.GetCapability("browserName");
var browserVersion = obj.Capabilities.GetCapability("version");
Console.WriteLine("Name: {0} | Version: {1}", browserName, browserVersion);
}
}
Upvotes: 1
Reputation: 108
Ok, based on your response in the comments, I think this is what you are going for.
The ChromeDriver
object has a Capabilities
property you can use to request the name and version.
As long as you are working with a ChromeDriver
directly and not an IWebDriver
that property is accessible like follows:
string? versions = driver.Capabilities.GetCapability("browserVersion").ToString();
string? name = driver.Capabilities.GetCapability("browserName").ToString();
I modified your program a little bit based on what it seems like you are trying to do and printed the result to the console:
private static ChromeDriver WebInit()
{
// set up options
ChromeOptions options = new ChromeOptions();
options.AddArguments("--start-maximized");
// download latest chromedriver
new DriverManager().SetUpDriver(new ChromeConfig(), VersionResolveStrategy.MatchingBrowser); //this is the code used from the mentioned Github Repository
// initialize the driver
ChromeDriver driver = new ChromeDriver(options);
return driver;
}
public static void Main()
{
ChromeDriver driver = WebInit();
// get the capabilities
string? versions = driver.Capabilities.GetCapability("browserVersion").ToString();
string? name = driver.Capabilities.GetCapability("browserName").ToString();
Console.WriteLine(versions);
Console.WriteLine(name);
Console.ReadKey();
}
The above example works for me in .Net 6.0 with the following NuGet packages installed:
Upvotes: 1