IHTMLDocument -- Click an IHTMLElement by ID, not Name - c#

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.

Related

ASP.NET WebForms WebSite - controls changes their's values in codebehind but not on frontend

I will try my best to express what I do not understand about WebForms. Maybe somone can explain it to me....
I work in 'quite big' WebForms (website) Application.
Application already has a PopUp (which I HAVE TO USE, because I dont have time to make a new one) like MessageBox in Winforms, but:
it's place is in MasterPage.master (I use it like ((MasterPage)Poup.Show("blablabla", yes_no));
I can add buttons to it and change style etc.
it doenst stop application (like .ShowDialog() in WinForms), so I have to assign onClick events dynamicly and assign/catch them on Page_Load to go to the wanted method depending on users input on form. At the moment i can't to anything about it.
And here is the problem:
Depending on form validation, if users press a button (on a form) - then form realods it's TextBox.Text's values and changes DropDownLists's SelectedIndexes and SelectedValues.
BUT IF YOU GO TO THE SAME METHOD WITH MY POPUP WINDOW:
ex: Do you confirm? YES/NO
If you press You press ANY button, then: CHANGES ARE VISIBLE BY CODEBEHIND BUT TEXTBOXES ARE STILL VISIBLE WITH PREVIOUS DATA ON THE SCREEN, ALSO DROPDOWNLISTS HAVE OLD VALUES SELECTED
BUT IF YOU PRESS A FORM BUTTON (WHATEVER WHICH), EVEN IF IT'S METHOD DOES NOTHING - ALL FORM WILL "RELOAD" AND HAVE PROPER DATA ON THE SCREEN.
(it's not about Runat="server" or AutoPostBack, I checked it)
I dont event know what to do about it :(
Update panels are used to partial refresh of some fields inside him, caused by certain triggers, like buttons, textchanged, etc.
See more about update panel's:
https://msdn.microsoft.com/en-us/library/bb399001.aspx
http://www.asp.net/web-forms/overview/older-versions-getting-started/aspnet-ajax/understanding-asp-net-ajax-updatepanel-triggers
http://www.asp.net/ajax/documentation/live/tutorials/IntroductionUpdatePanel.aspx
(Haven't worked with web forms events in a while, just answering in case nobody else does.)
Out of curiosity, what happens if, in your javascript handler for your popop button, you getElementById one of the form buttons on the main page and call ".click()" on it?
The basic problem sounds like there's a runat/server form on the main page, with certain behaviors associated with it, which is submitted by the main page runat server buttons but not by your popup JS.
Easy enough to test with the above approach. If that works, take a step further and try to figure out why it worked, based on what those buttons are doing.
(If you're not using JS at all for the popup, then View Source the output HTML, because I'm sure it is. Backtracking those calls should help you understand the trace from popup button press to form submission.)
I probably mislead you by "OnClick", it's NOT javascript OnClick="method()" issue.
I mean ClickEvent on a button (in codebehind). For Ex. If form is validated I have to do:
ViewState["PopUpSelection"] = "yes_no";
(MasterPage)PopUp.Show("blabla",yes_no) //loads popup from MasterPage;
return;
then on Page_Load I have:
**if (string)ViewState["PopUpSelection"] == "yes_no"
{
ButtonConfirmYes.Click += new EventHandler(MyMethod);
}**
And if method "MyMethod" is called from a FORM button, everything "will go ok".
If you call the same method from PopUp button: changes are visible by codebehind, but on the screen nothing happens, unless i press ANY FORM BUTTON, whatever it does

custom onbeforeunload alert/message

As many people, I tried to look for a solution to customize the onbeforeunload pop up window but I came up with no solution
So I was wondering, is it possible to hide this onbeforeunload (or make it not appear) and make a custom alert/pop up doing the same and customized?
You can't do that, if you add an alert to the onbeforeunload event that will give you a confirm message. if you were to stop that from happening, the page would just close no matter what you put there instead.
all you can do is add a message to the alert which is displayed when asking if you want to stay on the page or leave, even then that is ignored in some browsers.
function promptMessage() {
return "==============================\n\nINFO!!\n\n Please wait on this page until bla bla etc.\n\n==============================";
}
window.onbeforeunload = promptMessage;
The reason you can't and never will be able to do this is because it allows the site owner to stop someone from leaving or bombarding the end user when they try to leave.
Really, its a good thing that this is not allowed.
Myself, I do use onbeforeunload when a user has entered information into a form for example but has not saved that information when they try to leave or click on a link...
Onbeforeunload is a pain in th a** so I can of modify it (just the message) or know when it going to be fired (like clicking a button that goes to other page) and with javascript, disable it ( = null) and make a custom div that was hidden appear to look like a pop up.

mouseMove for Watin

I'm able to perform a mouse move over that triggers a menu down with Selenium's mouseMove function, but have codded most of my browser automation is Watin. Is there a suitable replacement for this option in Watin?
I've tried
div.FireEvent("onMouseMove");
div.MouseEnter();
with no luck. I target the same div classes. mouseMove for Selenium works, onmouseMove for Watin doesn't.
Working with some cranky form validation, I had to call the javascript change() method directly to get form validation to fire as Watin's element click() wasn't doing it.
Calling change() looked like this.
var jqElement = string.Format("window.jQuery({0})", GetJavascriptElementReference());
DomContainer.Eval(string.Format("{0}.change()", jqElement));
Obviously you'd need to change the window.jQuery... portion to point at your form, and change the change() method to the appropriate mouseover method you need called.
The negative here is the fragility due to calling a specific javascript method in your test code.
Another example of calling javascript directly can be seen in my answer here: Autocomplete DropDown Menu Testing using WatiN
Give mouse move a try...
div.FireEvent("mouseenter");
div.FireEvent("mousemove");

postback on click for type ahead drop down

I have a type a head drop down list that I created. Clearly I had to turn the AutoPostBack to false because otherwise it would post back as the user was typing. But I want to poast back when use has made his selection.
I don't wanna use a submit button or anything like that. Long story, just trust me it's not the solution for my situation.
In the javascript that does the type ahead when the use clicks enter I post back. That work great.
However whast about when the user click with the mouse? So I use the onblur event to postback. However that requires clicking outside of the drop down list.
I want the postback to happen when they click the new item. So like a postback when itemchanged, but only if I changed it by clicking on it.
Any ideas???
Providing your using an up to date version of Type Ahead, the library actually comes with a set of callback functions which are available here.
To execute a callback, just pass it in via a bind method when you initialize type ahead. Example code:
// attach type-ahead to class
$autoComplete.typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
displayKey: 'value',
source: substringMatcher($autoCompleteData)
}).bind("typeahead:selected", function (obj, datum, name) {
console.log(obj, datum, name);
});
The .bind method on the end will execute when the user either clicks an option or hits the return key on an option. Replace the console.log entry with whatever you want to do, i.e target a submit button a trigger a click.

How can I remove the input focus from textbox?

I'm making simple C# Winform application. There is a Form having a textbox. I want to change the location of textbox by arrow key but textbox has the input focus so form's KeyDown event is not called. How can I remove that input focus?
Typing on the textbox should still be possible. I try to make a dummy label and give the focus, but It doesn't work. If I press any key, the cursor go back to the textbox. please help me. How can I solve this problem?
Handle the TextBox.KeyDown event. And set e.Handled = true; in your handler after you move the TextBox, but before you return. And, yeah, only handle the arrow keys.
Hmm, not sure if I understand. If the user can type into the edit box, then it can have focus. If he clicks outside of it, on a blank are of the form, then it loses focus.
If you want to be able to 1) type into the edit box and 2) move the edit box, then you need a separate mechanism to enter "move mode".
I would suggest either a "click here to move selected control" button, or a right-click context menu on the control with a "move control option".
You would also have to conisder how the user indicates that moving has ended.
Hope this helps.
NOTE: I just realized this is not even an in-browser C# app. I guess disregard all of this. Serves me right for not reading carefully enough.
Use Javascript, in particular, I'd personally recommend jQuery.
They have pretty nicely documented their library: http://docs.jquery.com/Main_Page
For this particular task, you are going to want to bind some sort of key event (ie. keypress) and make sure to stop event propagation (so that you prevent the default response which is to be sent to be simply handled by the textbox element's default listener).
So, to give you an idea, if you want to change the location of the textbox using keypresses (maybe arrow keys), do something like this:
/* link the jQuery source to the HTML page with script tag */
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
/* short hand for $(document).ready(function() { ... */
$(function() {
$("#textbox_id").keypress(function(e) {
var $this = $(this); // store the #textbox_id element in $this
e.preventDefault();
switch (e.keyCode) {
// find the actual integer code for the up arrow
case UP_ARROW:
$this.animate({
top : '-=10px'
}, 100); // time in milliseconds to complete the animation
/* fill in the cases */
}
}
});
Okay, I hope you get the picture. Find out more about animate() and other jQuery functions at in the documentation at the link I provided above. Hope that helps!
NOTE: Obviously, preventing the default handling of events is a terrible idea in this case for accessibility reasons. Use your best judgement when selecting keypresses to trigger these moving events -- whatever you do, don't disallow users from moving around within the text they have in input fields.

Categories

Resources