Click here to Skip to main content
16,017,376 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi !

I have a problem.

I use the webbrowser class to login to a place.
After I have logged into the site, I want to visit a link.

The problem is when I have logged into the place, it won´t visit the link. If I show an messagebox after logged in and before I visit the link, it works.

The Code:
C#
//For login: 

webBrowser3.Document.GetElementsByTagName("input")[1].InnerText = "password";
            webBrowser3.Document.GetElementsByTagName("input")[0].InnerText = "username";
                       
webBrowser3.Document.GetElementById("selection").Focus();
            SendKeys.Send("{DOWN}");
            SendKeys.Send("{TAB}");
         SendKeys.Send("{Enter}");
//After this I being logged into the site


//After login
while (webBrowser3.ReadyState != WebBrowserReadyState.Interactive) Application.DoEvents();
         {
             Application.DoEvents();
         }

webBrowser3.Document.Links[15].InvokeMember("click");
//Clicking the link.


//If I put in a messagebox before the last link, I can visit the link, otherwise I dont get logged in, its like the webbrowser hasn´t finished loading the page.

Regards Joakim
Posted
Updated 1-Mar-11 1:39am
v2
Comments
Sunasara Imdadhusen 1-Mar-11 7:39am    
Applied code formatting!!
!Kerish11 1-Mar-11 7:52am    
Sorry about that, my first time posting a question.

1 solution

After you send keys, set a global variable say bool login_initiated=true;

Handle the link click invocation in the document completed event..

you need to add a event handler on some event like form load etc.

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

C#
protected void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (login_initiated == true)
    {
        webBrowser3.Document.Links[15].InvokeMember("click");
    }
}
 
Share this answer
 
Comments
Sandeep Mewara 1-Mar-11 9:19am    
Good answer... 5!
!Kerish11 1-Mar-11 9:26am    
Thank you alot. It works perfectly !.
Albin Abel 1-Mar-11 9:38am    
Thanks Sandeep.
You are welcome Kerish
!Kerish11 1-Mar-11 10:07am    
I might be stupid. After I press the link, I want to continue to another page from there, how can I do that ? I want to use something like "webBrowser1.Document.GetElementsByTagName("input")[1].InvokeMember("click");"
Albin Abel 1-Mar-11 10:25am    
I assume just the chaining of events. The same way after execute the hyperlink click set a variable say loading_second_page=true. Again in the document completed event handler check for this boolean, if true then execute your button click. So setting a global variable and document completed events chaining. So here instead of using IF you can use case statements.

Also instead of using select by tag and the index, why don't use select by ID?. Because the index are easy changeable in a website under modification

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900