1

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

  • I guess I'm confused -- why are you casting it to a Remote Web Driver? I think you normally initialize it like `_driver = new RemoteWebDriver(new Uri(driverUrl), options);` – user1024742 May 25 '22 at 15:48
  • ICapabilities capabilities = ((RemoteWebDriver)_driver).Capabilities; string browserName = capabilities.GetCapability("browserName").ToString(); string version = capabilities.GetCapability("browserVersion").ToString(); above is the whole code. like I want to get the browserversion and browserName from the driver that is initialized, hence I am casting it to a remote webdriver – Shyam Patadia May 25 '22 at 16:02
  • Doesn't look like WebInit() returns anything. (typo in your post?) Not sure you can type to a super class, but you shouldn't need to. Just call those methods. (if the webdriver interface type gets in the way, just type to Chromedriver from the beginning... or cast to Chromedriver, which is what it is anyway....) Chromedriver extends ChromiumDriver which extends RemoteWebDriver. – pcalkins May 25 '22 at 21:08
  • Correct My mistake, the WebInit method returns _driver object. It is initialized as: IWebDriver _driver; – Shyam Patadia May 26 '22 at 11:37

1 Answers1

1

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)
user1024742
  • 108
  • 2
  • 7
  • But as you pointed out I am initializing the _driver object as follows: IWebDriver _driver; – Shyam Patadia May 26 '22 at 11:38
  • But as you pointed out I am initializing the _driver object as follows: IWebDriver _driver; As it is a WebDriver object below will not work. string? versions = driver.Capabilities.GetCapability("browserVersion").ToString(); – Shyam Patadia May 26 '22 at 12:34
  • Is there a specific reason you need to initialize it as an IWebDriver? I don't think it's a particularly good idea, but you can cast your IWebDriver to a ChromeDriver before getting the capabilities: `ICapabilities capabilities = ((ChromeDriver)_driver).Capabilities;` – user1024742 May 26 '22 at 14:42
  • https://stackoverflow.com/a/71619568/13454085 this I found but still facing the same issue. – Shyam Patadia May 26 '22 at 15:10
  • It worked, Instead of casting it to RemoteWebDriver I casted it as Chromedriver and now its working fine. Thank you very much for your help, learned a new thing today. – Shyam Patadia May 26 '22 at 15:13