Clicking in Internet Explorer - c#

I have to write simple program for clicking in Internet Explorer, have started, but due to my little experience in programming, stucked. On the first page 11.html I need to click button with text "Sign1", on the next "Sign2"
//11.html
<input type="button" class="button" onclick="document.location='21.html'" value="Sign1">
//21.html
<input type="button" class="button" onclick="document.location='31.html'" value="Sign2">
What is wrong? Second button is not working.
Public Class Form1 Public ie = CreateObject("InternetExplorer.Application")
Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
ie.Visible = True
ie.navigate("http://....com/11.html")
Do
Loop Until ie.ReadyState = 4
Actions()
End Sub
Private Sub Actions()
Dim eInputs = ie.Document.GetElementsByTagName("input")
For Each eInput In eInputs
If eInput.GetAttribute("value") = "Sign1" Then
eInput.Click()
Exit For
End If
Next
Threading.Thread.Sleep(5000)
//here ie href is about:blank - WHY???
eInputs = ie.Document.GetElementsByTagName("input")
For Each eInput In eInputs
If eInput.GetAttribute("value") = "Sign2" Then
eInput.Click()
Exit For
End If
Next
End Sub
End Class

I can see two problems here:
Cyrillic alphabet:
If eInput.GetAttribute("value") = "Подписать2" Then
I don't think you can hard-code these characters into the source code,
because they will not be read by the compiler. Hence, it's highly possible that the getAttribute method is returning the correct string but then it's comparing it against a ?????? one so it doesn't find it. Maybe, you might want to get them from an external source (a txt file, a database etc.).
No waiting time between the two buttons:
As you say Second button is not working. (very broad definition: is doing anything? Is raising an error?), I assume the first is. Hence, it might be you found already a way to work-around the alphabetic problem.
So I think the issue is that you don't wait enough time between one click and the other, so the page is not loaded yet when you want to click the second button..
Basically:
1) You click the button 1 in the client (let's say Google Chrome);
2) The button 1 click calls an action in the back-end, so the server will start generate the new HTML document in which you look for button 2;
3)... however, you don't give the server any time for regenerating the page so you already look for the button 2, which probably does not exist yet in your client because the server did not end the regeneration.
So, that's why it doesn't work. If this is the case, add a command to wait some time before to look for the button 2. It's not clear in which language you're developing since you referenced all the .NET languages, but looking at the code I can recognize VB.NET.
Hence, to wait 5 seconds in VB.NET before running the second part of the code:
Threading.Thread.Sleep(5000)
to be put between the first and the second For Loop

Related

Update span from code behind ie

I am using the following back-end c# code to see if I need to update the text inside a span.
c#:
if (status1.InnerHtml != temp1)
{
status1.InnerHtml = temp1;
status1.Update();
}
html:
<span runat="server" id="status1">Status 1</span>
This works fine in chrome and firefox, but it has issues in ie.
Visually, this is what happens (only in ie):
first time:
second time:
Possible Source of Error
I noticed that status1.InnerHtml always returns Status 1; as in it never changes. This leads me to believe that this is why it is creating a second element.
This means that I need to find a way to get the the current value of the span, using something besides InnerHtml (runat="server" was supposed to solve this issue).
Looking at the code in ie, on initial load, it is displayed properly. However, the second time I execute the code,
it turns
<ext.net.direct.update id="status1"/>
<span id="status1">
Text - Transfer completed
</ext.net.direct.update/>
(Note: the closing span tag was removed)
into
<span id="el_status1_container">
<span id="status1">
Text - Transfer completed
<span id="status1">
Text - Transfer completed
</ext.net.direct.update/>
(Note: <ext.net.direct.update id="status1"/> gets removed from the code, an element with a duplicate ID is inserted)
Any pointers would be greatly appreciated! Thank you
The solution I came up with was to make a button that when clicked loads the statuses. Using javascript, I clicked the button when it is generated with this code found from this post.
function initialload() {
if (document.getElementById('Label1')) {
document.getElementById("Label1").click();
} else {
setTimeout(initialload, 15);
}
This works because it turns out ie was ignoring the initial span (weird, but hey, it's ie) and using the second one to update it.
This is a workaround, so if anyone has a better answer, please share!

Selenium Click() does not work with ASP.NET LinkButton

I'm Writing a Selenium text case for an ASPX page. I want to click this ASP.NET asp:LinkButton element:
<asp:LinkButton runat="server" OnClick="Test_Click" Text="Just try to click me" ID="testtest123"></asp:LinkButton>
Which appears like this in the HTML page generated by .NET:
<a id="testtest123" href="javascript:__doPostBack('testtest123','')">Just try to click me</a>
But while Selenium has no problem finding the element, the Click() does nothing. I get no element missing exceptions or timeouts, the testcase just runs on as if the click fired properly.
Here's the simple line for clicking the element;
Browser.Driver.FindElement(By.Id("testtest123")).Click();
This code works fine with other ASP elements such as buttons and text boxes. I use C# to write my test cases using the 64 bit webdriver and IE 11.
I researched the solutions here: ASP.Net LinkButton Prevent Postback Not Working - JavaScript but they do not seem to apply to my situation:
Any help would be greatly appreciated!
EDIT: I posted another thread about the issue on this website:
https://code.google.com/p/selenium/issues/detail?id=7846&can=8&colspec=ID%20Stars%20Type%20Status%20Priority%20Milestone%20Owner%20Summary
I had a similar problem where some controls were rendered as espected and other don't, so are you sure the link ID is "testtest123"?
You have to set Client ID Mode to static in order to make the HTML ID be the same as in the .NET (the default is inherit, but in the web.config it is generlly set to be AutoID).
<asp:LinkButton runat="server" OnClick="Test_Click" Text="Just try to click me" ID="testtest123" ClientIDMode ="Static"></asp:LinkButton>
If the rendered HTML is actually <A id=testtest123 then that is not valid HTML since the attribute does not have quotation marks around the value. If this is actual value then the problem is further upstream than Selenium
One possibility is that selenium is correctly clicking the element, but that the linkbutton's javascript code is not executing quickly enough for the rest of your test. This is because when selenium encounters an <input type="submit"> tag (like asp.net button controls), it knows that it has to wait for the form submission before going to the next test step. However, a linkbutton is just an <a> tag, and worse, it has javascript as the href as opposed to a url. So it's possible that the postback javascript takes 50 milliseconds to execute or whatever, in that case, it's possible a test like this:
step n: click linkbutton - selenium does not know how long this takes so proceeds to next step
step n+1: click some button - if this happens before the js finishes executing, it would be like step n did not happen at all
This is plausible in the case of selenium because it operates at the speed of a program, whereas a human tester would never be able to click the next step quickly enough to cause such a problem. The fix for this is to add a delay step, or, to add a watch step on a javascript variable that you set on click of your linkbutton.
Edit:
Ok, so then this is not the issue. I'm almost positive it has something to do with the javascript in the href, though, so could you try the answer here: https://stackoverflow.com/a/18847713/1981387 ?

SendKeys sends text to the wrong field out of two identical ones

I have a webpage with 2 search fields (top and bottom) and some text in-between them and I want to send a query to the bottom field. Both fields are identical (same code).
I tried storing the bottom one in a variable and use that variable to send the keys, but somehow it always sends the text to the first one.
var bottomSearch = _WebDriver.FindElements(By.Id("inputBox"))[1];
Assert.IsTrue(bottomSearch.Displayed);
bottomSearch.Clear();
bottomSearch.Click();
bottomSearch.SendKeys("test");
bottomSearch.SendKeys(Keys.Enter);
So: Clear() works and properly deletes any text already present in the bottom search, Click() also works, SendKeys sends text to the top searchbox and SendKeys(Keys.Enter) goes back to the bottom one and presses Enter from there.
I use the firefox driver and also tried selecting the element by CssSelectors or other identifiers but did not work.
Any help or ideas are most appreciated!
Thank you!
Here is the code for the search fields:
<div class="searchbox-input">
<input id="inputBox" class="querybox" type="text" placeholder="Entrer le terme recherché" name="inputBox" value="test">
</div>
OK, so after 2 days of trial and error I asked the website's devs to change the id of the bottomSearch and now everything works well.
After all these tries I'm tempted to suspect a bug in the driver or the SendKeys() method, but since I failed to found any reference to back this up and I successfully dealt with same ID fields before, maybe it's just something in the website's implementation or mine.
So in conclusion, follow the best practice advices and this should never happen :)

ASP.net help regarding Javascript and input from textboxes

Here's the basic flow of the webpage I'm working on:
User clicks a button, date /time picker pops up (works).
A time string fills a textbox, say, 4:00 AM (works).
User then has to select one of two radio buttons, which should read the time in the text box, radiobutton1 adds 4 hours, radiobutton2 adds 4.5 (does not work).
The total hours is then displayed in another textbox (haven't gotten to this yet).
Other than the 1st item (which retrieves the dates from a server), most of the work is done by Javascript.
So here's the Javascript I'm working on:
function setHours(setting)
{
var tempVal = document.getElementById('<%=Text6.ClientID%>');
var nHours;
var textHours = new DateTime(tempVal);
alert(tempVal);
textHours = getHours
if (setting==true) {
nHours = 4;
alert("4");
}
else {
nHours = 4.5
alert("4.5");
}
textHours.setHours(textHours.getHours() + nHours);
document.getElementById("text8").value = textHours;
}
This function is called when the user clicks on either one of the two radiobuttons (one passes true, the other passes false). Notice that I have "alert"s scattered throughout, because initially, I couldn't get the entire function to fire. So I commented out everything but the "if statements" and the "alert"s, and lo and behold it works fine.
I'm assuming the problem is somewhere in the var, perhaps, how "tempVal" retrieves the value in text6.
Here's the code for the textbox.
<form id='frmRequestApplicationForm' name='frmRequestApplicationForm' action='#' method='post'>
<input class="textboxdefault" type="text" name="Starttime" id="Text6" value="<%=(requestApplicationForm.StartimeError.ToString().Equals("")?requestApplicationForm.StarttimeDate.ToShortDateString().Equals("1/1/0001")?"":requestApplicationForm.StarttimeDate.ToShortTimeString(): requestApplicationForm.Starttime)%>"/>
</form>
Building the program nets me a "Text6 does not exist". I'm not sure where the code refuses to work.
Replacing "<%=Text6.ClientID%>" with a simple "Text6" makes the entire Javascript function fail.
Could I have some help on this?
Nevermind.
I replaced "document.getElementById('<%=Text6.ClientID%>');" with
document.frmRequestApplicationForm.Text6.value;
Seems to work fine now.

WebBrowser and javascript

I am working with a website that has javascript that does some changes on the page load. However, when I load the page and handle the DocumentCompleted event, this change isn't there. If I then continue paste the DocumentCompleted event, I can see the change happen. However I need this change to happen during DocumentCompleted so I can check some things.
Is there an other event I can subscribe to, or a way to cause the webBrowser to do all the javscript on page?
Edit: This is what I am talking about.
I loaded a sample page just to show you, and clicked the submit button with all fields empty to generate an the error.
Here is the result:
http://s8.postimage.org/zfv6stcar/sfsdfsdfds.jpg
Now if I take the HTML at that precise moment from that WebBrowser control, and render it somewhere else, those errors go away. The same thing happens when the server sends back those errors. If I handle the DocumentCompleted event and take the html, it isnt there. But after the event, it shows up in the control.
Hope you understand, it's hard to explain.
The problem seems to be that the DocumentCompleted event is being fired before the javascript. You should do some reading on how client side/server side things function.
One option is to make a separate method for the DocumentCompleted event and call it form the javascript after it has been completed. This would get the sequencing of these events working properly, but is not very ideal.
Alternatively, you could call the javascript code at the beginning of your DocumentCompleted event. The link below gives a pretty good explanation of how to go about that.
http://forums.asp.net/t/1117189.aspx/1
Personally, I would avoid using javascript and do the validation on the client side .NET, but I don't know enough about the website to really say.
EDIT:
This should be the script you are looking for. Alternatively here is a thread related to your issue. Sorry I don't have the exact code as I don't have a project to test this on.
http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx
Calling JavaScript Function From CodeBehind
RE-EDIT:
What is happening on the link you provided in the comments, is that each textbox is calling some javascript as well as the submit button. The best way to examine this is using the "Inspect Element" in the right-click menu on Google Chrome. For example, doing this on the textbox would show that it is registered with a few events:
onfocus="$('f_tip_Username').style.display = 'inline'"
onblur="$('f_tip_Username').style.display = 'none'"
onchange="$('f_err_Username').style.display = 'none'"
The first the element with the ID 'f_tip_Username', sets the display style of that element to inline (visible).
The submit button calls the following:
onclick="return o_edit_profile_form.validate()"
Doing a find on "o_edit_profile_form" in the source code, you can find the exact javascript location that is being called. Enjoy!
FINAL EDIT (hopefully?):
Follow these steps: go to your site, right click and go view source. Do a find for "f_tip_Username". This is the ID of one of the div tags being used. The third entry of it, should be a "div tag" that is used under the first textbox to warn of "min 3 characters".
You'll notice above that in the code is a input type "text" with the Name "Username". Notice the three events it has registered in it:
onfocus="$('f_tip_Username').style.display = 'inline'"
onblur="$('f_tip_Username').style.display = 'none'"
onchange="$('f_err_Username').style.display = 'none'"
These either hide or make visible, the div tag we found (f_tip_username) and also a separate div tag (f_err_Username) which is the error message div tag. Let me know if you are not able to find these in the source. Follow the steps I provided and you will find it in the "view source" OR in the DocumentText.

Categories