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.
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.
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.
Use 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:
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");
There are a few ways to do this with the standard WebBrowser control.
For HTML: If you want to fillout a textbox and then click submit then don't even bother with a keypress. 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.
**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"):
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.
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.
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.
Related
I'm creating an application which will capture a string from an USB attached scanner. I don't want a text box on the form I'm capturing the data on so I've added a handler to the KeyDown event of the window. There is a specific sequence of key presses I can look for to start capturing the data, however, what I can capture is a list of Virtualkeys. That would include LeftShift, v for 'V'
I'm looking for a way to take the array of Virtualkeys and convert that to a string.
Or if you can suggest another way to catpure the text, maybe hidden textbox?
UPDATE
I've positioned a textbox off window and was able to maintain keyboard focus on it so I could capture the data from the barcode scanner.
You can get info whether any VirtualKey is pressed using this:
bool isPressed = Window.Current.CoreWindow.GetKeyState(VirtualKey).HasFlag(CoreVirtualKeyStates.Down);
AFAIK this is the best way of getting to know whether more keys are pressed at the same time.
I have a program with a timer which sends the message "Test" each second via sendkeys, and I have it setup to where it will sendkeys 5 times, then disable the timer!
But I'm wondering if there is a way to only make it send the message if a valid input for it is selected, because it keeps trying to sendkeys when I don't have an input selected(EG: Notepad), and that is not really the desired result!
Not sure I fully understand your situation, but you can check to see if the active control is what you expect it to be by calling
If Screen.ActiveControl.Name <> "MyControl> Then ...
End If
before the SendKeys is executed. You can also set the focus to a control you need it to be focused to with:
Me!YourControlName.SetFocus
I want to get the input value of an <input type="text"> element, on the fly (while it is being typed) and implement a search method with it as parameter.
I have this piece of code:
_window.Frame(WatiN.Core.Find.ById("a_frame"))
.TextField(WatiN.Core.Find.ById("an_element"))
.FireEventNoWait("event_string", other params);
What event would you think is the best suited for this? I have some thoughts on KeyPressed or KeyUp, but I'd like some other opinions for this matter? I have searched for TextChanged and some similar Event, but I haven't found anything.
Are we to assume the above code isn't working, or? If nothing else works you could always do a do loop right after the text box gets focus, do loop would contain these:
Sleep 100
Doevents
And of course after every 100 ms break and a Doevents, you can check to see if .value has changed, and if so, query your search. When the text box looses focus, you stop the loop.
You are using the webbrowser control, right? And you want to d this using the webbrowser control as opposed to JavaScript? Because, you can get keyup keydown events through the webbrowser controls eventing system, and that would be a better way to do it, but I'm not clear on the who what when where why of what your doing :)
I have a string value in a variable. I want it to be thrown in keypress event.
When user clicks on "Start Writing Button". The text contained in variable gets written to the area whereever cursor has focus.
eg.
string str = "Example"
I have a web page with a textbox and a button. When user clicks on Start Reading button Example gets written on to textbox.
Basically the characters being written should be trapped on
javascript-onKeyPress event
Winform- KeyPress event
etc.
EDIT
I want to use some devices that will be throwing data constantly to my variable using window service. I need to write this data to the active window whereever the cursor has focus currently irrespective of window or web.
I copied data to clipboard and pasted on active window but this is problematic since different tabs are considered to same active window and doesn't writes.
Looking for a proper way rather then workaround I have taken.
Now that you've clarified the question, I suspect you want SendKeys.SendWait.
You'll need to be somewhat careful with it, but it may do what you want.
I want the 'Alt' to be pressed by code. It is like firing key-press event with key 'Alt' by the code, not hitting it manually. The need is, I have set 'ShortCut keys for menu, but it (the single underline on key letter) is not visible to the user unless he presses 'alt'. So i need to make the Alt be pressed by default.
Is there a way to 'press' or 'fireup' the keys in key board using c# code?
Check out the System.Windows.Forms.SendKeys class.
You can use the static Send method to send keystrokes to the active window. If you're trying to send keystrokes to another window, you'll need to use the Windows API to activate the other window first.
If you have any control over the operating system on which the program is being deployed, apparently you can force the underlined shortcut letter to always be displayed by going to Control Panel -> Display -> Appearance -> Effects -> Hide underlined letters for keyboard navigation.
(http://www.chinhdo.com/20080902/underlined-letters-windows/)
here is a complete article on use of SendKeys on codeproject
Here is how you simulate input (both mouse and keyboard).
http://msdn.microsoft.com/en-us/library/ms171548.aspx
If you look at the System.Windows.Forms.SendKeys class you will see that it provides you with what you want.
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx