c# - 我为什么可以将 IWebDriver 转换为 IJavaScriptExecutor?

标签 c# javascript .net inheritance selenium

谁能解释一下这个“魔法”?

public interface IWebDriver : ISearchContext, IDisposable
{
    string CurrentWindowHandle { get; }
    string PageSource { get; }
    string Title { get; }
    string Url { get; set; }
    ReadOnlyCollection<string> WindowHandles { get; }

    void Close();
    IOptions Manage();
    INavigation Navigate();
    void Quit();
    ITargetLocator SwitchTo();
}

public interface ISearchContext
{
    IWebElement FindElement(By by);
    ReadOnlyCollection<IWebElement> FindElements(By by);
}

public interface IJavaScriptExecutor
{
    object ExecuteAsyncScript(string script, params object[] args);
    object ExecuteScript(string script, params object[] args);
}

怎么会

(this.Driver as IJavaScriptExecutor).ExecuteScript("console.log('wtf?')");

工作正常吗?

最佳答案

这是因为main implementation IWebDriver implements IJavaScriptExecutor 从而提供实现 of those methods .

您尚未提供显示如何实例化驱动程序的代码,但它们都继承自 RemoteWebDriver:示例是 ChromeDriver

关于c# - 我为什么可以将 IWebDriver 转换为 IJavaScriptExecutor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23112045/

相关文章:

javascript - Angular 工厂模块获取 promise 数据

C# DirectoryInfo.GetFiles 通配符搜索

c# - 使用 ATL/COM 将托管字节 [] 转换为非托管字节数组

c# - C# 'is' 和 'as' 关键字的 Java 等价物是什么?

c# - 自动实现属性的代码契约(Contract)

c# - 使用 Core 2.2 本地化的 Razor 页面不起作用

c# - 如何恢复/中断 BackgroundWorker 线程

c# - 超过 SMTP 限制发送电子邮件

javascript - 分享使用 JavaScript 生成的 map

javascript - 我的 google map api 脚本出了问题