SendKeys.send与Internet Explorer

时间:2015-02-12 14:42:15

标签: c#

以下命令无法在Internet Explorer 8中正确输出。

SendKeys.Send("Testing");

我还尝试将文本复制到剪贴板并模拟ctrl-v,但这也不起作用。

将数据输入自动化到IE 8的正确方法是什么

2 个答案:

答案 0 :(得分:1)

如果打开Internet Explorer窗口,则可以输入文本的唯一位置是输入控件,例如文本框或组合框等。您的SendKeys语句基本上尝试做的是覆盖网页上显示的现有内容。

如果要测试sendkeys的功能,请使用此示例在记事本文本编辑器中尝试:

    Dim w As New SendKeys.WINFOCUS
    w = SendKeys.GetWinHandles("New Text Document - Notepad", 1, "Edit", 1)
    SendKeys.Send("Hello", w)

该代码段来自codeguru:SendKeys discussion and examples

答案 1 :(得分:1)

以www.google.com

的文本框中的数据输入为例
SHDocVw.InternetExplorer IE=new InternetExplorer();
object URL = "www.google.com";
IE.Visible = true;
IE.Navigate2(ref URL);
System.Threading.Thread.Sleep(2000); // You can put some custom conditions here such as ready state/document complete/page complete events as an indicator when the page has completely loaded 
mshtml.HTMLDocument doc=(mshtml.HTMLDocument)IE.Document;
IHTMLInputElement textBox = (IHTMLInputElement)doc.getElementById("lst-ib"); //lst-ib is the unique id of the textbox in which you want to set a value
textBox.value = "Testing";