在C#中使用Selenium IE驱动程序来快速发布大量文本(10,000行) [英] Using Selenium IE driver in C# to quickly post a large amount of text (10,000 lines)

查看:123
本文介绍了在C#中使用Selenium IE驱动程序来快速发布大量文本(10,000行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我目前正在编写此脚本,该脚本将使用C#中的Selenium Internet Explorer驱动程序自动执行简单,单调的任务.

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.

问题是当我必须在文本框中填写很多信息时.有时,此文本框将被填充多达10,000行,每行不得超过20个字符.

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?

感谢您的时间和耐心!

推荐答案

因此,正如Richard所建议-我最终使用了IJavaScriptExecutor.

So as suggested by Richard - I ended up using IJavaScriptExecutor.

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

The exact solution was to replace the call to SendKeys in the following line of code:

inputTextBox.SendKeys(stringBuilder.ToString());

使用以下代码行:

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

将我的InternetExplorerDriver对象投射到IJavaScriptExecutor接口,以便到达显式实现的接口成员ExecuteScript,然后使用上面的参数对该成员进行调用就可以了.

Casting my InternetExplorerDriver object to the IJavaScriptExecutor interface in order to reach the explicitly implemented interface member ExecuteScript and then making the call to that member with the arguments above did the trick.

这篇关于在C#中使用Selenium IE驱动程序来快速发布大量文本(10,000行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆