سؤال

The input field that I need to fill in has it's name value set to j_password. The button I would like to click is a link within a div that has an id set to loginBtn.

I'm trying something like this:

mshtml.IHTMLDocument2 doc = ((mshtml.IHTMLDocument2)webBrowserControl.Document);
((mshtml.IHTMLElement)doc.all.item("j_password")).setAttribute("value", password);
((mshtml.IHTMLElement)doc.all.item("loginBtn")).click();

But the button is never clicked. I can't tell if it is because it isn't really a button, or if it is because it doesn't have a name attribute and I'm trying to use an id.

Anyway, does anyone see a fix to this?

هل كانت مفيدة؟

المحلول

If the click call did not throw a NullReferenceException, then you found the element. But it is better to use the standard IHTMLDocument3::getElementById orIHTMLDocument3::getElementsByName instead of document.all for the future.

As for why click does not work, it depends on how click is supposed to work. There are a lot of side effects of a mouse click, and the browser may limit those effects if the mouse is not over the element between a mouse down and a mouse up.

If the click is supposed to call a javascript function, just call the function directly (ExecScript). Otherwise, find the login form element, call its onsubmit handler function if there is one, then call its submit method.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top