Winform App - Webpage Interaction
Asked Answered
S

2

6

Windows Form Application – Manipulating input-elements in WinForm WebBrowser

Although I am familiar with HttpWebResponse/HttpWebRequest to login to a website, I was trying it now via using the mshtml library and found some weird behavior and I would like to see if someone else might be able to help me out here..

I have an HTML login page with a java backend with a Username field, a Password field and a Button.

The logic is very basic, I have a built a winform app with a built in webbrowser. At the Document_Completed event I use the following code to enter my settings and to click the button.

private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (webBrowser.Url.ToString() == @"MyWebPage/signin")            
    {
        HTMLDocument hdc = new HTMLDocumentClass();
        hdc = (HTMLDocument)webBrowser.Document.DomDocument;

        IHTMLElement elb = hdc.getElementById("login_button");
        IHTMLInputElement elu = (IHTMLInputElement)hdc.getElementById("username");
        IHTMLInputElement elp = (IHTMLInputElement)hdc.getElementById("password");

        try
        {
            elu.value = "MyID";
            elp.value = "MyPwd";
            elb.click();
        }
        catch { }
    }
}

Apart for this code being very quick and without error handling, it should do the trick and it does, partially..

There are two scenario's:

  • I launch the tool, it loads the webpage.

    • The tool populates the UserID field and the Password field correctly
    • The tool fails to click the button
  • I click the button manually, I am logged in, I click logout, I am back at login page

    • I immediatly logged in again, the tool enters the information
    • The tool immediatly clicks the button as well.

Is there anyone who might be able to explain me why this happens and how I could get around this with the current setup (hence not using HttpWebRequest). I don't see the difference between loading the page at startup or being redirected after logout, but apparently there is a difference in there or I am doing something wrong.

Any feedback on this matter is very much appreciated.

Thanks, Kevin

EDIT:

I added a Button to my Windows Form that bas the same backend Code as below in order to click the button on the webpage, this works perfectly.

I triggered clicking this button in the webBrowser_Completed event but it doesn't work. For some reason, everything I add to the webBrowser_DocumentCompleted event does not allow me to trigger the click event for the button in my WebBrowser control. Once that entire event has completed, if I then try to trigger it it works but I would like to automate this.. Any advice?

Stamford answered 11/5, 2012 at 9:16 Comment(9)
Seems to be you shouldn't make click in document completed for first scenario. But let me ask, why you need it at all? May be I could suggest alternative wayJotunheim
Regfor, this is for an application we're building at work, the logic uses a lot of HTML interaction and I tried to stay in the same line. But I'm always open to suggestions. ThanksStamford
Ok, I've thought it's some kind of testing or test tool for Web. Think interating with DOM using WebBrowser has so many undocumented pitfalls. So, when I need something like you are doing in Web testing or Web automation context, than I've used Watin watin.orgJotunheim
have you tried to give delay in the document completed function, give it a delay of about 2 seconds...and then try fire click event !Tiffaneytiffani
@Ovais Khatri Delay is workaround. Most likely this stuff should be done not in DocumentCompleted event handlerJotunheim
@OvaisKhatri The delay is indeed a workaround but a good tip, I however tried this as well without any luck. I have checked everything and can't seem to put my finger on the difference between after logout and the first launch of the page.Stamford
@Jotunheim Watin is indeed a good tip, but I assume this should do the same handling as the manual way. I will give it a try and see if it could make any sort of difference. I however would like to be able to figure out what makes it work after logout and not prior to login. Thanks a lot for the feedback so far.Stamford
@KevinLaerte Watin has opened source code, therefore you can look at how they have already solved same problems watin.svn.sourceforge.net/viewvc/watinJotunheim
@Jotunheim superb, I'll look into it and see if Watin can enlighten me or if I could just use it as is. Thanks a lot for your feedback so far, it was really helpfull.Stamford
B
3

This might be a long shot and not the most elegant workaround but how about letting a backgroundworker run for a second in your DocumentCompleted event that then triggers the button that you clicked from it's seperate thread. This might just get this automated. As this will run from a different thread, keep in mind that you might have to invoke certain controls so this might be another downside to this workaround..

If this doesn't work then, as Regfor previously suggested, Watin.org can help you out.

Benzo answered 21/5, 2012 at 13:43 Comment(1)
It actually works, I put in a bgw that slept for about a second and then triggered the button_onlcick event I had on the form and it worked perfectly.. It is a workaround indeed and I really would like to figure out why it happens but at least I can continue the rest of the app first now. Thanks a lot.Stamford
J
3

how about this :

HtmlElement button = webBrowser.HtmlDocument.GetElementById("login_button");
button.InvokeMember("click");

it works in my program.

Jack answered 11/5, 2012 at 9:49 Comment(5)
Thanks, I'll give it a try and keep you posted.Stamford
I tried it but the issue stays the same. When I open my app and the page loads, it fills in the userdetails but fails to click the button. I click it manually, I get logged in. On the website in my app, I click logout, the page redirects me back to login and in about 2 seconds I am logged on again because it does what it should do. Am really having a hard time trying to figure out what the difference is between the two scenarios. Thanks for the feedbackStamford
you can check if the button is really the "login_button"Jack
Just checked via the debugger, it is indeed the correct button because I can for example change the text on it, that works, it is just the onclick event that doesn't work. however it does work when I am being logged out in the tool, then it is able to automatically connect so it does execute it, but just not at the primary launch.. As Ovais suggested, I'll try to add a timeout.Stamford
The logic you wrote down should indeed work, it does about the same, I think this is really webpage related. Thanks for the feedback.Stamford
B
3

This might be a long shot and not the most elegant workaround but how about letting a backgroundworker run for a second in your DocumentCompleted event that then triggers the button that you clicked from it's seperate thread. This might just get this automated. As this will run from a different thread, keep in mind that you might have to invoke certain controls so this might be another downside to this workaround..

If this doesn't work then, as Regfor previously suggested, Watin.org can help you out.

Benzo answered 21/5, 2012 at 13:43 Comment(1)
It actually works, I put in a bgw that slept for about a second and then triggered the button_onlcick event I had on the form and it worked perfectly.. It is a workaround indeed and I really would like to figure out why it happens but at least I can continue the rest of the app first now. Thanks a lot.Stamford

© 2022 - 2024 — McMap. All rights reserved.