Jump to content
  • Advertisement
Sign in to follow this  
0x0539

[.net] C# WebBrowser Macros

This topic is 3182 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Would HtmlDocument.InvokeScript work? Untested code demonstrating the general idea (beware; my HTML/javascript 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");

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!