Sign in to follow this  

[.net] C# WebBrowser Macros

This topic is 2842 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
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
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

This topic is 2842 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this