Friday, September 24, 2010

C# | Automating Facebook Login using WebBrowser Control

Create a new windows forms application project.

Add two button and place the web browser control as shown below. Rename the button as “btnShowPage” and “btnLogin”.

image

On form load event use the following code

private void Form1_Load(object sender, EventArgs e)
{
btnLogin.Enabled = false;
}



Now on button btnShowPage use the following code



private void btnShowPage_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("https://login.facebook.com/login.php?login_attempt=1");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}


void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string s = webBrowser1.DocumentText;
btnLogin.Enabled = true;
}



On btnLogin click event use the following code



private void btnLogin_Click(object sender, EventArgs e)
{
HtmlElement ele = webBrowser1.Document.GetElementById("email");
if (ele != null)
ele.InnerText = "amalhashim@gmail.com";

ele = webBrowser1.Document.GetElementById("pass");
if (ele != null)
ele.InnerText = "password";

ele = webBrowser1.Document.GetElementById("Login");
if (ele != null)
ele.InvokeMember("click");
}



That’s it :-)

5 comments:

  1. Hi ,

    the website that i wish to access usin a similar method has only the following


    input name="user" type="text" size="15"


    ie .. there is not id . how should i proceed in this case ?

    ReplyDelete
  2. @Vivek M: In that case you should use GetElementsByTagName. Which will return a collection of tag's say input in your case. Iterate through the collection and find the exact element.

    ReplyDelete
  3. Hi,
    I am trying to login to a website. I can set attributes like username and password but Login button neither has id nor it has name. Can you please suggest how can get around this to invoke click in this situation.

    Regards,
    MSH

    ReplyDelete
  4. Hi,
    I am trying to login to a website. I can set the username and password but the login button does not have id or name element can you please suggest how can I manage to invoke click in this scenario.

    Regards,
    MSH

    ReplyDelete
  5. I've encountered that kind of situation (missing ID) and the following way worked perfect for my case. In my case, the login button is very close to the input boxes, so I've used:
    HtmlElement elem = webBrowser1.Document.GetElementById("userNameInput");
    //done some filling stuff as I've learnt from this page, thankfully
    elem = webBrowser1.Document.GetElementById("passwordInput");
    //done filling
    elem.Parent.NextSibling.InvokeMember("Click");

    I've found how must I choose the elem.Parent.NextSibling code by using chrome's inspect element function, I saw I must go up once in that tree and go to the next element once. So I did wrote elem.Parent.NextSibling and it accessed the login button. I think you may find a nearby element which has an ID. Hope it helps. Thanks dear author!

    ReplyDelete