I have an application that uses WebBrowser control to go to a website, and try to log in. I found a few questions that seem similar but are not the same, and their answers do not help.
The website prompts for the information in steps, first prompts for the username, then prompts for the challenge question, then prompts for the password. To debug, I've created three buttons, one for each step above, so that I can separate to see what works and what doesn't. (In the end this will be fully automated, without any user interaction).
The first button navigates to the site and enters the username and submits -- that works fine. The second button is supposed to enter the answer to the challenge question, but the site returns saying that the entry is invalid. The actual HTML looks like this:
<input name="IDToken1" tabindex="1" id="IDToken1" style="padding: 2px; border: 2px solid rgb(240, 0, 0); border-image: none; width: 226px; color: black;" type="password" size="40" maxlength="40" value="" autocomplete="off">
The code to enter the challenge question looks like this:
var browser = (WebBrowser)_interactor.WinForm.Controls["browser"];
var txt = browser.Document.GetElementById("IDToken1");
if (txt != null)
{
txt.SetAttribute("Value", "answer-to-question");
// set event handler on click
//btnContinue.AttachEventHandler("onclick", (sender, args) => OnElementClicked(txt2, EventArgs.Empty));
var btnLogin = browser.Document.GetElementById("signIn");
btnLogin.InvokeMember("click");
}
I verified that the value is properly set by adding an event handler to the submit button, then checking the input value at the point of the form submission. The value looks to be set at that point, but when the form is submitted it returns an error, stating that the value is not valid.
I've also tried entering it by submitting the actual form, like so:
browser.Document.Forms["challengequestion"].Children["IDToken1"].SetAttribute("Value", "answer-to-question");
browser.Document.Forms["challengequestion"].InvokeMember("submit");
I get the same response.
I've also tried doing the cache clear as suggested here, which does not seem to help.
Finally, I've tried manually entering the answer and clicking the submit button, via the WebBrowser control on the application.
What am I missing? If I check the value of the input element on form submit and it has the proper value, how is it that the site is not able to see it? I enter the same value manually on the control and it works fine. Any ideas?