在不可见的网络浏览器对象 C# 中模拟按键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/915177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:05:56  来源:igfitidea点击:

Simulate keypress in a non-visible webbrowser object C#

c#.nethtmlajaxbrowser

提问by Artanis

I'm currently working in an application that has to navigate a webpage and recollect data about products, prices, ... with webbrowser object in .net 3.5.

我目前正在使用一个应用程序,该应用程序必须使用 .net 3.5 中的 webbrowser 对象导航网页并重新收集有关产品、价格等的数据。

The problem is that the page has some fields with a suggest script in ajax, and I can't simply modify the innerText property because the script also saves codes in a hidden input field.

问题是页面有一些字段带有 ajax 中的建议脚本,我不能简单地修改 innerText 属性,因为脚本还将代码保存在隐藏的输入字段中。

I need a way to simulate the typing in that field and then send the "Enter" key, or launch the ajax script and then send the "Enter" key, or any better ways to do this.

我需要一种方法来模拟该字段中的输入,然后发送“Enter”键,或启动 ajax 脚本然后发送“Enter”键,或任何更好的方法来执行此操作。

采纳答案by Mischa Kroon

Use Watin

使用Watin

Then you can use this solution.

然后你可以使用这个解决方案

回答by Beatles1692

To submit a form or run a script you can do this: If you know the script name you can use InvoekScript of Document object:

要提交表单或运行脚本,您可以这样做:如果您知道脚本名称,您可以使用 Document 对象的 InvoekScript:

myWebBrowser.Document.InvokeScript("script-name",null);

the second argument is an array of objects to pass parameters values.

第二个参数是传递参数值的对象数组。

if you know the name of an element that it's click event fires the script you can do this:

如果您知道它的点击事件触发脚本的元素的名称,您可以执行以下操作:

HtmlElement element=myWebBrower.Document.GetElementById("element-name")[0];
element.InvokeMember("click");

回答by Eyal

There are a few ways to do this with the standard WebBrowser control.

有几种方法可以使用标准的 WebBrowser 控件执行此操作。

For HTML:If you want to fillout a textbox and then click submit then don't even bother with a keypress. Do this:

对于 HTML:如果你想填写一个文本框,然后点击提交,那么甚至不用按键。做这个:

webbrowser1.Navigate("javascript:function%20E(){f0=document.forms[0];f0['login'].value='foo';}E()")
webbrowser1.Navigate("javascript:document.forms[0].submit()")

This will be much more reliable then trying to send keypresses for HTML.

这将比尝试为 HTML 发送按键更可靠。

**For Flash:**If there's a flash element on the webpage that needs clicking then it won't work. In that case SendKeys is reliable but only sends to the active application so it won't work in the background. You can send windows messages like this (example will press the letter "f"):

**对于 Flash:**如果网页上有需要点击的 flash 元素,那么它将不起作用。在这种情况下 SendKeys 是可靠的,但只发送到活动应用程序,因此它不会在后台工作。您可以像这样发送 Windows 消息(示例将按字母“f”):

Dim c as char = "f"c
Dim classname As New System.Text.StringBuilder(100)
Dim ExplorerHandle As IntPtr = webbrowser1.Handle
GetClassNameA(ExplorerHandle, classname, classname.Capacity)
Do While classname.ToString <> "Internet Explorer_Server"
    ExplorerHandle = GetWindow(GetExplorerHandle, GetWindow_Cmd.GW_CHILD)
    GetClassNameA(ExplorerHandle, classname, classname.Capacity)
Loop

PostMessageA(ExplorerHandle, WM_Constants.WM_KEYDOWN, IntPtr.Zero, IntPtr.Zero)
PostMessageA(ExplorerHandle, WM_Constants.WM_KEYUP, CType(VkKeyScanA(CType(Asc(c), Byte)), IntPtr), IntPtr.Zero)

You can find the definitions for PostMessage, GetClassName, GetWindow, etc, online at pinvoke.net. Notice that the WM_KEYUP uses c but the WM_KEYDOWN sends a dummy key (0). KEYDOWN and KEYUP have to go in pairs or else the key won't be registered, but if you hold down Control while sending, for example, KEYDOWN "p", it will activate IE's print function. For all the letters and digits you can send 0 for KEYDOWN and the correct letter for KEYUP. Backspace seems to need a real KEYDOWN, not 0, but Control-Backspace doesn't seem to do much in IE so if c = vbBack, KEYDOWN needs to be different.

您可以在 pinvoke.net 在线找到 PostMessage、GetClassName、GetWindow 等的定义。注意 WM_KEYUP 使用 c 但 WM_KEYDOWN 发送一个虚拟键 (0)。KEYDOWN 和 KEYUP 必须成对使用,否则密钥将不会被注册,但是如果您在发送时按住 Control,例如 KEYDOWN "p",它将激活 IE 的打印功能。对于所有字母和数字,您可以为 KEYDOWN 发送 0,为 KEYUP 发送正确的字母。Backspace 似乎需要一个真正的 KEYDOWN,而不是 0,但 Control-Backspace 在 IE 中似乎没有多大作用,所以如果 c = vbBack,KEYDOWN 需要不同。

The keypresses aren't very accurate, either, and 1 time in about 500 it misses. But you can do it with the window minimized, no problem, and a standard WebBrowser control.

按键也不是很准确,大约 500 次中就有 1 次错过。但是您可以通过最小化窗口和标准的 WebBrowser 控件来实现,没问题。

Another option is to use AxWebBrowser. The ActiveX control seems to avoid the Control-P problem but it's not as easy to manipluate because it isn't a nice .Net control.

另一种选择是使用 AxWebBrowser。ActiveX 控件似乎避免了 Control-P 问题,但操作起来并不容易,因为它不是一个很好的 .Net 控件。