使用SendKeys函数后C#Selenium Web驱动程序异常

时间:2014-09-03 14:40:33

标签: c# selenium web driver

您好,我在使用SendKeys功能后遇到了我的网络驱动程序问题。

我要做的是在点击网页上的“保存”按钮后单击对话框上的“确定”按钮,然后尝试获取新打开的页面的网址。

在使用SendKeys功能之前,我的网页驱动程序运行正常,因为我可以点击按钮,获取网址等。

我认为问题可能与点击对话框上的确定按钮后失去焦点的网页有关,所以我使用了以下代码(但这也不起作用): -

> System.Collections.ObjectModel.ReadOnlyCollection<string> winHandle2 =
> _webDriver.WindowHandles;
> 
> _webDriver.SwitchTo().Window(winHandle[0]).SwitchTo();

我得到的例外可以在下面看到: -

WebDriver.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理 其他信息:[JavaScript错误:“a为空”{file:“file:/// C:/Users/andrew.short/AppData/Local/Temp/anonymous.3779fc41a91f475c89d01937ed7bb71b.webdriver-profile/extensions/fxdriver@googlecode。 com / components / command_processor.js“line:8166}]'[JavaScript错误:”a为空“{file:”file:/// C:/Users/andrew.short/AppData/Local/Temp/anonymous.3779fc41a91f475c89d01937ed7bb71b .webdriver-profile / extensions / fxdriver @ googlecode.com / components / command_processor.js“line:8166}]'调用方法时:[nsICommandProcessor :: execute] 如果存在此异常的处理程序,则可以安全地继续该程序。

我感谢任何人都能给我的任何帮助: - )

1 个答案:

答案 0 :(得分:0)

public static void SendKeys(this IWebElement element, string value, bool clearFirst)
{
    if (clearFirst) element.Clear();
    element.SendKeys(value);
}

这个功能非常简单,只需简单地使我们的代码变得有点简单。在最初的Selenium RC中,SendKeys将清除文本字段,然后发送所需的文本。在Selenium 2中,除非明确执行,否则不会先清除文本字段。然后我们可以将它组合起来以允许类似:

myElement.SendKeys("populate field", clearFirst: true);