13 Replies - 1486 Views - Last Post: 04 May 2011 - 06:40 AM Rate Topic: -----

#1 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

DocumentCompleted event is tripping

Posted 01 May 2011 - 08:18 AM

I have a Login() function that I use for a website login.

private void Login()
        {
            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(CheckLogin);

            foreach (HtmlElement e in wb.document.All)
            {
                if (e.Name == "username") e.InnerText = sUser;
                if (e.Name == "password") e.InnerText = sPass;
                if (e.GetAttribute("value") == "Login") e.InvokeMember("Click");
            }
        }


The CheckLogin() function checks if the login was successful or not.

private void CheckLogin(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (wb.ReadyState == WebBrowserReadyState.Complete)
            {
                wb.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(CheckLogin);

                if (wb.Url.Equals("http://www.just-a-website.com/error.php"))
                {
                    MessageBox.Show("Login unsuccessful. Please review your login credentials.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    rtb.AppendText(wb.Url.ToString() + "\n");
                }
                else
                {
                    MessageBox.Show("Login successful.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

                    rtb.AppendText(wb.Url.ToString() + "\n");
                }
            }
        }


The first page my form navigates to, BEFORE login, is: http://www.just-a-website.com/index.php

If you enter a wrong login on the website, it navigates to: http://www.just-a-website.com/error.php where it says invalid username/password
If the login is correct, it navigates to: http://www.just-a-website.com/welcome.php

I added the line below in my if-else statement to check the CURRENT url whether or not the login was successful.

rtb.AppendText(wb.Url.ToString() + "\n");


Problem: My form does not do the above. For some odd reason, if I use incorrect login details, it shows the error.php page saying invalid username/password, but my form says the login is successful. This is where the line above comes in useful. It writes http://www.just-a-website.com/index.php, but the page is on http://www.just-a-website.com/error.php. Ofcourse it says the login is successful...

A few months ago, I programmed two similar applications. Three weeks ago, both were still working flawless. Because of my current problem, I tested both yesterday, and they are doing the same thing: wrong login details -> login successful ??

Thank you for any help. Desperate...

This post has been edited by skater_00: 01 May 2011 - 08:27 AM


Is This A Good Question/Topic? 0
  • +

Replies To: DocumentCompleted event is tripping

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5517
  • View blog
  • Posts: 11,826
  • Joined: 02-June 10

Re: DocumentCompleted event is tripping

Posted 01 May 2011 - 08:41 AM

We have a tutorial for this...


Password handling tutorial
Was This Post Helpful? 0
  • +
  • -

#3 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: DocumentCompleted event is tripping

Posted 01 May 2011 - 10:59 AM

View PosttlhIn`toq, on 01 May 2011 - 05:41 PM, said:

We have a tutorial for this...


Password handling tutorial


I'm sorry, but to be honest, that doesn't really help me.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5517
  • View blog
  • Posts: 11,826
  • Joined: 02-June 10

Re: DocumentCompleted event is tripping

Posted 01 May 2011 - 11:38 AM

Quote

but my form says the login is successful.


Let's think about it logically.

To get to the 'else' clause (where this message is displayed) of your if...else it must be the condition of the if portion fails.

Therefore wb.url does not exactly equal the string you entered.

Put a breakpoint on line 7 and check the actual value of wb.url
I think you'll find it doesn't match exactly, thus the condition of your if fails so it falls through to the else clause.
Was This Post Helpful? 0
  • +
  • -

#5 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: DocumentCompleted event is tripping

Posted 01 May 2011 - 11:48 AM

View PosttlhIn`toq, on 01 May 2011 - 08:38 PM, said:

Quote

but my form says the login is successful.


Let's think about it logically.

To get to the 'else' clause (where this message is displayed) of your if...else it must be the condition of the if portion fails.

Therefore wb.url does not exactly equal the string you entered.

Put a breakpoint on line 7 and check the actual value of wb.url
I think you'll find it doesn't match exactly, thus the condition of your if fails so it falls through to the else clause.


Inserted the breakpoint, and it says wb.Url is http://www.just-a-we...e.com/index.php
The displayed page however is http://www.just-a-we...e.com/error.php

So confusing... Why is it doing that?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5517
  • View blog
  • Posts: 11,826
  • Joined: 02-June 10

Re: DocumentCompleted event is tripping

Posted 01 May 2011 - 12:00 PM

I have no idea about that site. Forwarding... frames... Because the web designer wanted it that way so outsiders weren't eating up all their bandwidth and resources... so that people have to actually use the proper navigation of their site instead of trying to hardcode links to it... ???

The important thing is that you've a little about how to debug your applications, and can now explain the unexpected behavior.
Was This Post Helpful? 0
  • +
  • -

#7 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: DocumentCompleted event is tripping

Posted 03 May 2011 - 08:28 AM

I'm currently trying this with the Microsoft Web Browser COM component (AxWebBrowser)

There's a small bit of code in my Login() function I'm having trouble with.

foreach (HtmlElement e in wb.document.All)
            {
                if (e.Name == "username") e.InnerText = sUser;
                if (e.Name == "password") e.InnerText = sPass;
                if (e.GetAttribute("value") == "Login") e.InvokeMember("Click");
            }


I get these 2 errors:

Quote

error CS0446: Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'?
error CS1579: foreach statement cannot operate on variables of type 'method group' because 'method group' does not contain a public definition for 'GetEnumerator'


How do I get the foreach statement to work with this AxWebBrowser? Anyone able to help me out?
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5517
  • View blog
  • Posts: 11,826
  • Joined: 02-June 10

Re: DocumentCompleted event is tripping

Posted 03 May 2011 - 09:25 AM

foreach (HtmlElement e in wb.document.All)



The messages are telling you that are are trying to use a method like it was a container.

You can use foreach on things like Form1.Controls because .Controls is a container of items and can be enumerated. It's basically a property.

But you couldn't for example do a foreach on a method like .Print()
It just would make sense.

Same thing here. You are trying to use a method like it was an enumerable counter.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4488
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: DocumentCompleted event is tripping

Posted 03 May 2011 - 10:10 AM

All is a method, not a property. It does return an enumerable container, so you can iterate through it, but you have to call the method. Add () after All.
Was This Post Helpful? 0
  • +
  • -

#10 skater_00  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 12
  • View blog
  • Posts: 256
  • Joined: 30-April 08

Re: DocumentCompleted event is tripping

Posted 04 May 2011 - 06:05 AM

View PostCurtis Rutland, on 03 May 2011 - 07:10 PM, said:

All is a method, not a property. It does return an enumerable container, so you can iterate through it, but you have to call the method. Add () after All.


error CS1501: No overload for method 'All' takes 0 arguments

:dontgetit:
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5517
  • View blog
  • Posts: 11,826
  • Joined: 02-June 10

Re: DocumentCompleted event is tripping

Posted 04 May 2011 - 06:24 AM

SO look on the MSDN as to how you should use it.
You really aren't making any effort of your own.
You get a problem, you ask us to solve it but do not even look it up.

Or look at the Intellisense as you are typing .All.Add(
and see what it tells you that you are supposed to use for parameters.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5517
  • View blog
  • Posts: 11,826
  • Joined: 02-June 10

Re: DocumentCompleted event is tripping

Posted 04 May 2011 - 06:37 AM

I don't know if this helps or even applies but using a WeBrowser component seems to work just fine when you fix your capitalization from .document to .Document

Quote

foreach (HtmlElement e in wb.document.All)




.All returns a collection and does not require parenthesis.

You do need to perform a check to confirm that .Document is not null.

And all of this was redily available by just looking at the Intellisense messages that pop up when you over over a given item like .Document or .All

Attached Image

This post has been edited by tlhIn`toq: 04 May 2011 - 06:39 AM

Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4488
  • View blog
  • Posts: 7,816
  • Joined: 08-June 10

Re: DocumentCompleted event is tripping

Posted 04 May 2011 - 06:38 AM

I believe he's using the AxWebBrowser (the COM Active X version) instead of WebBrowser, the managed .NET version.

This post has been edited by Curtis Rutland: 04 May 2011 - 06:41 AM

Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5517
  • View blog
  • Posts: 11,826
  • Joined: 02-June 10

Re: DocumentCompleted event is tripping

Posted 04 May 2011 - 06:40 AM

Debugging tutorial
Debugging tips
Great debugging tips
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1