Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
I am actually working on a login project for a specific website, instead of going to the website to enter the login credentials (Username an Password) It would be much easier for my customer to enter those credentials from my Windows Forms Application C#. I have accomplished this using the built in feature of Windows Forms Application using the WebBrowser Control. However, I want to go further and do this in a customer's default web browser rather than taking advantage of the Web Browser Control that is inside WinForms Application. DropBox has done this in their Windows and Mac applications. How can I do this?



The following code implemented in C# with WebBrowser Control feature enabled:

Username box taken from the site: "_ctl0_PlaceHolderMain_Loginapp2_txtLoginUsername"
Password box taken from the site: "_ctl0_PlaceHolderMain_Loginapp2_txtLoginPassword"

Login Button taken from the site: "_ctl0_PlaceHolderMain_Loginapp2_btnLoginLogin"

C#
using System.Windows.Forms.HtmlElement;


HtmlElementCollection theElementCollection;
                    theElementCollection = Browser.webBrowser1.Document.GetElementsByTagName("input");
                    foreach (HtmlElement curElement in theElementCollection)
                    {
                        if ((curElement.GetAttribute("id").ToString() == "_ctl0_PlaceHolderMain_Loginapp2_txtLoginUsername"))
                        {
                            curElement.SetAttribute("Value", id.Text);
                        }
                        else if ((curElement.GetAttribute("id").ToString() == "_ctl0_PlaceHolderMain_Loginapp2_txtLoginPassword"))
                        {
                            curElement.SetAttribute("Value", pw.Text);

                        }
                    }
                    theElementCollection = Browser.webBrowser1.Document.GetElementsByTagName("input");

                    foreach (HtmlElement curElement in theElementCollection)
                    {
                        if (curElement.GetAttribute("id").Equals("_ctl0_PlaceHolderMain_Loginapp2_btnLoginLogin"))
                        {
                            curElement.InvokeMember("click");

                        }
                    }
Posted
Comments
[no name] 14-Jul-14 15:07pm    
http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx
[no name] 14-Jul-14 18:59pm    
Thanks Wes Aday, but I'm asking for the implementation, not an overview.
[no name] 14-Jul-14 19:01pm    
Right. As you have been told before, we don't write code to order.
[no name] 14-Jul-14 19:10pm    
Well why did CodeProject existed if that's the case?
Nguyen.H.H.Dang 14-Jul-14 21:29pm    
CodeProject - For those who code, not for a lazy look for someone to code something for him

1 solution

There is no universal method for doing this. Not every browser exposes an API to use to inject values into a loaded page.
 
Share this answer
 
Comments
[no name] 15-Jul-14 16:24pm    
How come DropBox has done something similar to what I was asking for?
Dave Kreskowiak 15-Jul-14 23:48pm    
I don't use DropBox.

Probably because they went through the pain of researching every browsers support for this an implemented separate support modules for each browser they wanted to support.
[no name] 16-Jul-14 14:28pm    
Hmmm you're right

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