EventFiringWebDriver不会通知事件

时间:2016-05-23 23:15:20

标签: c# selenium

操作系统:Windows 10 Selenium版本:2.53.0 浏览器:Chrome 浏览器版本:50.0.2661.102m

预期行为 - 应该回复下面的代码示例

实际行为 - 单击导航的链接元素时,不会调用任何事件处理程序。

重现步骤 - 在Visual Studio中创建一个selenium项目,导航到您喜欢的站点,在从gotoURL方法调用返回webdriver后调用此代码。

下面的StartWebDriver,打开一个新的WD实例并导航到返回Webdriver的url。我们在Chrome中看到了新的网址。接下来我们调用HookEventFiringWebDriver ......并且永远不会被通知。

    var wd = StartWebDriver(url);
    wd.HookEventFiringWebDriver(changedURL => {});

这里是帮助这个事件的辅助类,我尝试了不同的方法来挂钩但没有工作。

public static void HookEventFiringWebDriver(this IWebDriver wd, Action<string> NavigatedToURL) {
    var ed = new EventFiringWebDriver(wd);
    ed.ElementClicked += new EventHandler<WebElementEventArgs>((o, e) => {
        Debugger.Break();
    });
    ed.Navigated += (driver, ea) =>
    {
        Debugger.Break();
        if (NavigatedToURL != null) NavigatedToURL(ea.Url);
    };
    ed.ElementClicked += (driver, ea) =>
    {
        Debugger.Break();
        if(NavigatedToURL !=null) NavigatedToURL(ea.Element.Text);
    };
    ed.Navigating += (driver, ea)=>{
        Debugger.Break();
        if (NavigatedToURL != null) NavigatedToURL(ea.Url);
    } ;
    ed.ExceptionThrown += (driver, ea) =>
    {
        Debugger.Break();
    };
} -->

3 个答案:

答案 0 :(得分:1)

EventFiringWebDriver类是一个扩展类,旨在拦截对WebDriver实例的调用。因此,要通知事件,您需要调用EventFiringWebDriver实例中的方法。

以下是一个例子:

var driver = new EventFiringWebDriver(new FirefoxDriver());

driver.Navigated += new EventHandler<WebDriverNavigationEventArgs>((obj, ea)=>{
    Debugger.Break();
});

driver.Navigate().GoToUrl("http://stackoverflow.com/");

答案 1 :(得分:0)

为了任何想要一种功能性方式来连接的人的利益 EventFiringWebDriver使用C#和可选的回调...

    /// <summary>
    /// Allows for events to be posted back to the user from the browser
    /// </summary>
    /// <param name="wd">The WebDriver to hook into</param>
    /// <param name="Error">Any Exceptions if thrown</param>
    /// <param name="Navigating">On Navigation start</param>
    /// <param name="NavigatedToURL">On Navigation complete</param>
    /// <param name="Clicked">On web element clicked</param>
    /// <param name="HookedDriver">The Hooked driver</param>
    /// <returns>The Hooked driver</returns>
    public static IWebDriver HookEventFiringWebDriver(this IWebDriver wd,
        Action<WebDriverExceptionEventArgs> Error = null,
        Action<WebDriverNavigationEventArgs> Navigating = null,
        Action<WebDriverNavigationEventArgs> NavigatedToURL = null,
        Action<WebElementEventArgs> Clicked = null,
        Action<IWebDriver> HookedDriver = null)
    {
        var ed = new EventFiringWebDriver(wd);
        ed.ElementClicked += new EventHandler<WebElementEventArgs>((o, ea) =>
        {
            if (Clicked != null) Clicked(ea);
        });
        ed.Navigated += (driver, ea) =>
        {
            if (NavigatedToURL != null) NavigatedToURL(ea);
        };

        ed.Navigating += (driver, ea) =>
        {
            if (NavigatedToURL != null) NavigatedToURL(ea);
        };
        ed.ExceptionThrown += (driver, ea) =>
        {
            if (Error != null) Error(ea);
        };
        if (HookedDriver != null) HookedDriver(ed);
        return ed;
    }

以下是如何使用它。

        CurrentWebDriver.HookEventFiringWebDriver(
            error => { 
                //process errors here 
            },
            navigating => {
               //browser is navigating
            },
            navigated => { 
              //browser has navigated 
            },
            clicked => { 

             //element was clicked
            }, 
            wd => {
                wd.Navigate().GoToUrl(url);
            });

答案 2 :(得分:0)

另一个注意事项:EventFiringWebDriver(在C#中)包含对EventFiringWebElement(EFWE)的私有引用。如果将它作为IWebElement传递给另一个方法,它将隐藏获取Wrapped驱动程序的能力。这是揭露它的方法......

public static IWebElement ScrollIntoView(this IWebElement ele){
        var rele = ((IWrapsElement)ele).WrappedElement as RemoteWebElement;
        var wd = rele.WrappedDriver;
        var action = new Actions(wd);
        action.MoveToElement(ele);
        action.Perform();
        return ele;
    }