Agent_Spock
Agent_Spock

Reputation: 1197

c# Win Form - Unable to change text of an "input" in WebBrowser control

I am using Webbrowser control to login this page by using the following code

webBrowser1.Navigate(new Uri("https://services.gst.gov.in/services/login"));

Now for changing the password I am using the below code which isn't working.

webBrowser1.Document.GetElementById("user_pass").InnerText = "ABC123";
//OR
webBrowser1.Document.GetElementById("user_pass").SetAttribute("type", "text");
webBrowser1.Document.GetElementById("user_pass").SetAttribute("text", "ABC123");
//OR
webBrowser1.Document.GetElementById("user_pass").SetAttribute("text", "ABC123");
//OR
webBrowser1.Document.GetElementById("user_pass").SetAttribute("value", "ABC123");

Can anyone tell me how to do so?

Upvotes: 2

Views: 1708

Answers (2)

Adriano Repetti
Adriano Repetti

Reputation: 67148

You first have to wait that the page is fully loaded:

webBrowser1.DocumentCompleted += OnDocumentLoaded;
webBrowser1.Navigate(new Uri("https://services.gst.gov.in/services/login"));

Now you can set value attribute (which contains the content of <input> elements):

private void OnDocumentLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.Document.GetElementById("user_pass")
        .SetAttribute("value", "the_password");    
}

As alternative you may also send keystrokes:

    webBrowser1.Document.GetElementById("user_pass").Focus();
    SendKeys.Send("the_password");

Upvotes: 3

praty
praty

Reputation: 573

The field you are trying to set is a password type field and not text type field. This is not a good way to do it but I may suggest you do it it this way:

webBrowser1.Document.GetElementById("user_pass").SetAttribute("value", "ABC123");

Upvotes: 0

Related Questions