简体   繁体   English

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

[英]C# Selenium Web Driver Exception after using SendKeys Function

Hi I am having problems with my web driver after using the SendKeys function. 嗨,使用SendKeys函数后,我的Web驱动程序出现了问题。

What I am trying to do is after clicking an OK button on a dialogue box after clicking a 'Save' button on a webpage I am then trying to get the newly opened page's url. 我想做的是在单击网页上的“保存”按钮后,在对话框中单击“确定”按钮后,然后尝试获取新打开页面的URL。

My web driver is functioning correctly before using the SendKeys function, as I am able to click buttons, get urls etc. 使用SendKeys函数之前,我的Web驱动程序可以正常运行,因为我可以单击按钮,获取url等。

I thought the issue might have been to do with the web page losing focus after clicking the OK button on the dialogue box so I used the following code (but this did not work either):- 我认为问题可能与单击对话框上的“确定”按钮后网页失去焦点有关,所以我使用了以下代码(但这也不起作用):

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

The exception I am getting can be seen below:- 我得到的异常可以在下面看到:

An exception of type 'System.InvalidOperationException' occurred in WebDriver.dll but was not handled in user code Additional information: [JavaScript Error: "a is null" {file: "file:///C:/Users/andrew.short/AppData/Local/Temp/anonymous.3779fc41a91f475c89d01937ed7bb71b.webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js" line: 8166}]'[JavaScript Error: "a is null" {file: "file:///C:/Users/andrew.short/AppData/Local/Temp/anonymous.3779fc41a91f475c89d01937ed7bb71b.webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js" line: 8166}]' when calling method: [nsICommandProcessor::execute] If there is a handler for this exception, the program may be safely continued. WebDriver.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理。其他信息:[JavaScript错误:“ a为空” {文件:“ file:/// C:/Users/andrew.short /AppData/Local/Temp/anonymous.3779fc41a91f475c89d01937ed7bb71b.webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js“行:8166}]'[JavaScript错误:“ a为null” {file:” file: ///C:/Users/andrew.short/AppData/Local/Temp/anonymous.3779fc41a91f475c89d01937ed7bb71b.webdriver-profile/extensions/fxdriver@googlecode.com/components/command_processor.js“行:8166}]'在调用方法时: [nsICommandProcessor :: execute]如果存在用于此异常的处理程序,则可以安全地继续执行该程序。

I appreciate any help anybody can give me :-) 我感谢任何人都能给我的帮助:-)

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

This function is very simple, all it does is make our code a little from concise. 这个函数非常简单,它所做的只是使我们的代码简明扼要。 In the original Selenium RC, SendKeys would clear a text field and then send the required text. 在原始的Selenium RC中,SendKeys将清除文本字段,然后发送所需的文本。 In Selenium 2, text fields are not cleared first unless explicitly executed. 在Selenium 2中,除非明确执行,否则不会首先清除文本字段。 We can then combine it to allow something like: 然后,我们可以将其组合以允许类似以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM