Generate click event in input button in Windows.Forms.WebBrowser c

G

Guest

I've seen a similar question, but no answer yet.

I am automating some web interactions using the Windows.Forms.WebBrowser
control in a Windows Forms C# program. I can find input text fields in the
webBrowser.document and fill in the desired values. I can also find the Input
sumbitButton in the document.

If I manually click on the submitButton displayed in the WebBrowser control,
the webform and website act as expected, however, the two ways I have tried
to automate clicking on the submit both fail.

If I use:
webBrowser1.Document.Forms[0].InvokeMember("Submit");
only part of the correct web client work happens, and my web site responds
with an error page saying I need to enable javascript.

When i try:
// find the submit button
HtmlElement submitButtonHtmlElement = null;
HtmlElementCollection inputHtmlCollection =
webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement anInputElement in inputHtmlCollection)
{
if (anInputElement.Name.Equals("submitButton"))
{
submitButtonHtmlElement = anInputElement;
}
}
// press this button -- this is the part I'm having trouble figuring out!
submitButtonHtmlElement .RaiseEvent("submit");

-- trying to raise the submit event, and trying to raise a "click" event
both throw argument out of range exceptions.

What method can I use to generate a click event on the submit button in the
loaded document in the embedded webBrowser control so it acts the same as
manually clicking the visible submit button in the control?

Thanks,
 
W

Walter Wang [MSFT]

Hi Jim,

The IHTMLElement in DHTML DOM (Document Object Model) has a method named
"click" to manually invoke the "Click" event. However, .NET WebBrowser's
HTMLElement is a wrapper around this interface and doesn't create a wrapper
for this "click" method. We need to use its InvokeMember to dynamically
call this method instead:

submitButtonHtmlElement.InvokeMember("click");


Hope this helps.


#IHTMLElement Interface ()
http://msdn2.microsoft.com/en-us/library/aa752279.aspx



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top