简体   繁体   English

在不可见的webbrowser对象中模拟按键C#

[英]Simulate keypress in a non-visible webbrowser object C#

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”键,或者更好的方法来执行此操作。

Use Watin 使用Watin

Then you can use this solution . 然后你可以使用这个解决方案

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: 要提交表单或运行脚本,您可以执行以下操作:如果您知道脚本名称,则可以使用InvoekScript of Document对象:

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: 如果你知道它的click事件触发脚本的元素的名称,你可以这样做:

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

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. 对于HTML:如果要填写文本框然后单击“提交”,则不要使用按键。 Do this: 做这个:

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. **对于Flash:**如果网页上有需要点击的flash元素,则无法使用。 In that case SendKeys is reliable but only sends to the active application so it won't work in the background. 在这种情况下,SendKeys是可靠的,但只发送到活动应用程序,因此它不能在后台运行。 You can send windows messages like this (example will press the letter "f"): 您可以发送这样的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. 您可以在pinvoke.net上在线查找PostMessage,GetClassName,GetWindow等的定义。 Notice that the WM_KEYUP uses c but the WM_KEYDOWN sends a dummy key (0). 请注意,WM_KEYUP使用c,但WM_KEYDOWN发送一个虚拟键(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. KEYDOWN和KEYUP必须成对出现,否则密钥将不会被注册,但如果您在发送时按住Control,例如KEYDOWN“p”,它将激活IE的打印功能。 For all the letters and digits you can send 0 for KEYDOWN and the correct letter for KEYUP. 对于所有字母和数字,您可以为KEYDOWN发送0,为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. Backspace似乎需要一个真正的KEYDOWN,而不是0,但是Control-Backspace似乎在IE中没有那么多,所以如果c = vbBack,KEYDOWN需要不同。

The keypresses aren't very accurate, either, and 1 time in about 500 it misses. 按键也不是非常准确,并且在未错过的约500次中有一次。 But you can do it with the window minimized, no problem, and a standard WebBrowser control. 但是你可以通过窗口最小化,没问题和标准的WebBrowser控件来实现。

Another option is to use AxWebBrowser. 另一种选择是使用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. ActiveX控件似乎避免了Control-P问题,但它并不容易操作,因为它不是一个很好的.Net控件。

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

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