需要在Selenium C#中使用EventFiringWebDriver和OnFindingElement或OnElementClicked的工作示例吗?

时间:2015-04-30 17:09:12

标签: c# selenium selenium-webdriver onclicklistener

我是Selenium和C#的新手。有人可以帮我解释如何在单击元素或使用EventFiringWebDriver查找元素时如何将消息写入日志文件或Console.WriteLine?我在寻找C#而不是Java。

1 个答案:

答案 0 :(得分:17)

使用语句:

using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.Events;
using OpenQA.Selenium.Support.Extensions;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;

初始化WebDriver:

IWebDriver webDriver = new FirefoxDriver();
var firingDriver = new EventFiringWebDriver(WebDriver);

firingDriver.ExceptionThrown +=
    new EventHandler<WebDriverExceptionEventArgs>(firingDriver_ExceptionThrown);

firingDriver.ElementClicked +=
    new EventHandler<WebElementEventArgs>(firingDriver_ElementClicked);

firingDriver.FindElementCompleted +=
    new EventHandler<FindElementEventArgs>(firingDriver_FindElementCompleted);

webDriver = firingDriver; // this is important!

记录方法:

static void firingDriver_ExceptionThrown(object sender, WebDriverExceptionEventArgs e)
{
    Console.WriteLine(e.ThrownException.Message);
}

static void firingDriver_ElementClicked(object sender, WebElementEventArgs e)
{
    Console.WriteLine(e.Element);
}

static void firingDriver_FindElementCompleted(object sender, FindElementEventArgs e)
{
    Console.WriteLine(e.FindMethod);
}