[.net] C# WebBrowser Macros

Started by
3 comments, last by mutex 14 years, 1 month ago
Hello, I'm writing a C# program to automate accessing a website. I need help trying to submit a form, _without_ focus on the application window! Right now I'm using this code to set input values and submit the form:
webBrowser.Document.All["elemnt-id"].InnerText = "set text";
webBrowser.Document.All["sumbitLogin"].Focus();
SendKeys.Send("{ENTER}");
This works great except SendKeys will only work when the window is focused, is there a way I can do this without the window being focused? Like, is there a function I can call on the login button to tell it to submit? If so, I would still like to know how to send keyboard and mouse events to the form while it's not focused because I might start interacting with Flash controls in the future.
Advertisement
Would HtmlDocument.InvokeScript work? Untested code demonstrating the general idea (beware; my HTML/&#106avascript is beyond rusty):
<html><head><script type="text/javascript">function submitform() {  document.getElementById('myform').submit();}</script></head><body><form id="myform">...</form></body>C#:webBrowser.InvokeScript("submitform");


Edit: An alternative is HtmlElement.InvokeMember:
C#:webBrowser.Document.All["myform"].InvokeMember("submit");

Awesome, that works great! To be honest I saw InvokeMember, but I probably assumed it was like Invoke on controls and looked passed it..

I am still wondering if it's possible to send keyboard and mouse events to an unfocused window if anyone knows.
Well, I think you should be able do it with SendMessage(), but it's probably a bad idea. If the user scrolled a single line, things would stop working so..
I agree with DvDmanDT. Injecting input is a fragile approach that will fail if there's any timing issues, unexpected errors, UI, or user input that interferes with your input.

This topic is closed to new replies.

Advertisement