diff --git a/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs b/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs index 0a62e1467228a..9d8f03511c062 100644 --- a/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs +++ b/dotnet/src/webdriver/BiDi/WebDriver.Extensions.cs @@ -18,6 +18,7 @@ // using OpenQA.Selenium.BiDi.Modules.BrowsingContext; +using System; using System.Threading.Tasks; #nullable enable @@ -28,11 +29,18 @@ public static class WebDriverExtensions { public static async Task AsBiDiAsync(this IWebDriver webDriver) { - var webSocketUrl = ((IHasCapabilities)webDriver).Capabilities.GetCapability("webSocketUrl"); + if (webDriver is null) throw new ArgumentNullException(nameof(webDriver)); - if (webSocketUrl is null) throw new System.Exception("The driver is not compatible with bidirectional protocol or it is not enabled in driver options."); + string? webSocketUrl = null; - var bidi = await BiDi.ConnectAsync(webSocketUrl.ToString()!).ConfigureAwait(false); + if (webDriver is IHasCapabilities hasCapabilities) + { + webSocketUrl = hasCapabilities.Capabilities.GetCapability("webSocketUrl")?.ToString(); + } + + if (webSocketUrl is null) throw new BiDiException("The driver is not compatible with bidirectional protocol or \"webSocketUrl\" not enabled in driver options."); + + var bidi = await BiDi.ConnectAsync(webSocketUrl).ConfigureAwait(false); return bidi; }