0

So I'm currently writing this script that will automate a simple, monotonous task using the Selenium Internet Explorer driver in C#.

Everything works great, but it is a tad bit slow at one point in the script and I'm wondering if there is a quicker way available to do what I want.

The point in question is when I have to fill out a textbox with a lot of information. This textbox will be filled sometimes up to 10,000 lines where each line never exceeds 20 characters.

However, the following approach is very slow...

// process sample file using LINQ query
var items = File.ReadAllLines(sampleFile).Select(a => a.Split(',').First());

// Process the items to be what we want to add to the textbox
var stringBuilder = new StringBuilder();
foreach (var item in items)
{
     stringBuilder.Append(item + Environment.NewLine);
}

inputTextBox.SendKeys(stringBuilder.ToString());

Is there any to just set the value of the textbox to what I want? Or is this a bottleneck?

Thank you for your time and patience!

4

2 回答 2

1

所以正如理查德所建议的那样 - 我最终使用了IJavaScriptExecutor.

确切的解决方案是替换以下代码行中的调用SendKeys

inputTextBox.SendKeys(stringBuilder.ToString());

使用这行代码:

((IJavaScriptExecutor)ieDriver).ExecuteScript("arguments[0].value = arguments[1]",
                                          inputTextBox, stringBuilder.ToString());

将我的InternetExplorerDriver对象转换为IJavaScriptExecutor接口以访问显式实现的接口成员ExecuteScript,然后使用上面的参数调用该成员就可以了。

于 2013-09-24T20:31:44.013 回答
0

为避免使用 SendKeys,您可以使用 IJavaScriptExecutor 直接设置值。这些方面的东西:

string javascript = "document.getElementById("bla").value = stringBuilder";
IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript(javascript);
于 2013-09-24T18:12:12.187 回答