Home > OS >  Unable to cast object of type 'OpenQA.Selenium.Chrome.ChromeDriver' to type 'OpenQA.S
Unable to cast object of type 'OpenQA.Selenium.Chrome.ChromeDriver' to type 'OpenQA.S

Time:05-27

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

CodePudding user response:

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:

  • Selenium.Support (4.1.1)
  • Selenium.WebDriver (4.1.1)
  • WebDriverManager (2.13.0)
  • Related