YAM
YAM

Reputation: 1382

Problem logging in programatically to a website using WebBrowser Control

I'm trying to login programatically to https://www.salesgenie.com/Account/LogOn using WebBrwoser control.

The problem is when I click on "Log On", the browser doesn't navigate to the next page in the LogonCompleted event.

HtmlElement userName = wBrowser.Document.GetElementById("username");
userName.SetAttribute("value", SomeUserName);

HtmlElement password = wBrowser.Document.GetElementById("password");
password.SetAttribute("value", SomePassword);

wBrowser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(LogonPageLoaded);
wBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(LogonCompleted);

HtmlElement logonForm = wBrowser.Document.GetElementById("logon-submit");
logonForm.InvokeMember("click");

I think this is because the element "logon-submit" calls a JavaScript function or so.

Please, any help would be appreciated.

Upvotes: 1

Views: 1988

Answers (2)

Santiago
Santiago

Reputation: 43

Did you try using logonForm.click(); ? Without invoking any member ?

Upvotes: 0

Chris Haas
Chris Haas

Reputation: 55427

Check out your 4th line, you're setting userName again when you should be setting password

EDIT

Besides the problem above I'm guessing that you're trying to invoke the click too soon. The button is created using JavaScript so you have to wait a bit before clicking it. Unfortunately there's no event that you can listen for to determine when the JavaScript is done although you could test various properties probably. The safest thing is to probably just wait a couple seconds after load before invoking click.

The code below works for me (although I don't have a valid username and password). I've got one button called button1 and one webbrowser called webBrowser1. Once the page is visually loaded in the browser clicking the button on the form correctly invokes the click event in the browser.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.webBrowser1.Navigate("https://www.salesgenie.com/Account/LogOn");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string SomeUserName = "Test";
            string SomePassword = "Test";

            HtmlElement userName = webBrowser1.Document.GetElementById("username");
            Console.WriteLine(userName.GetAttribute("value"));
            userName.SetAttribute("value", SomeUserName);
            userName.RemoveFocus();

            HtmlElement password = webBrowser1.Document.GetElementById("password");
            Console.WriteLine(userName.GetAttribute("value"));
            password.SetAttribute("value", SomePassword);

            HtmlElement logonForm = webBrowser1.Document.GetElementById("logon-submit");
            logonForm.InvokeMember("click");
        }
    }
}

Upvotes: 2

Related Questions