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

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 :)

Related

How to locate the input box using C# Selenium

I need to find the input box in this HTML:
<div id="employeesDataTable_filter" class="dataTables_filter">
<label>
<input type="search" class="form-control input-sm"
placeholder="Filter..." aria-controls="employeesDataTable">
</label>
</div>
But for the life of me cannot - please help,
I have successfully written bags of tests and found many page element of different types but this one has stumped me.
I am very new to this and have tried
By ExecutiveSearchBox = By.XPath("//input[#type='search' and
class='dataTables_filter']");
You have encountered problems because you are selecting class attribute on input node instead on div. Try following selector:
//div[#class='dataTables_filter']//input[#type='search']
Also as #Marco Forberg mention it is good to use contain() XPath function in case if there are multiple classes provided for element:
//div[contains(#class, 'dataTables_filter')]//input[#type='search']
I hope it'll help to resolve your issue :)
To find the input element in your html snippet, you simply use
FindElement( By.CssSelector( "input" ) )
But note:
not always is the input box editable after page load is completed, it may take some time. It might be wise to wait until the box becomes editable if you want to send data to it.
not always does the input box appear immediately in the DOM. With modern UI like Angular, it might be not there immediately, might be something else for a while and only later become an input field and the like. Also here, making use of Seleniums wait functionality sure is a good idea.
I ALWAYS wait for the DOM state I expect and only after some time when the state is not achieved I throw.

Fill a form with WatiN in C#

that's my first question, i hope i'm doing everything correctly.
Anyway, i have a weird issue. I basically have this HTML input which i want to fill with WatiN :
<input name="edit[id]" class="form-text required" id="edit-id" type="text" size="60" maxlength="64" value="">
That's my code in C# :
IE ie = new IE();
ie.GoTo(urlhere, just too long);
ie.TextField(Find.ByName("edit[id]")).TypeText("Text");
It's not working and it returns this exception :
Ulteriori informazioni: Could not find INPUT (hidden) or INPUT (password) or INPUT (text) or INPUT (textarea) or TEXTAREA element tag matching criteria: Attribute 'name' equals 'edit[id]' at about:blank
I'm not sure why it gives "at about:blank" honestly. I tried with a random google page and the code it's working. If i put a random name instead of the correct one, it gives me the same exception but with the correct url instead of "about:blank".
Note : I'm trying to fill a form.
Thanks for your help!
Not sure what web page are you using but from the information you posted it looks like that form might be "somewhere else" (another div, frame, etc.) To be able to find the EXACT entry you should press F12 (IE) or CTRL+SHIFT+I (Chrome) and traverse the HTML web page to find the exact portion were the input lies and you will find the exact spot.
The code you posted is perfectly fine, there's nothing else you need to do to make it work. If you need more help, posting the webpage you are trying to use will go a long way (if possible, of course).

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!

Having trouble clicking on an element in an automated test

I am currently building a test harness for the company I work at. I have experience both with C# and WatiN and have never encountered the issue I am now having.
Below, is a snippet of the markup for the page giving me the issue:
<div id="toggle1" class="NavLayout toggle">
<span onClick="toggleMenu(1, false);">
<span id="toggletext1">Quote Processing</span>
</span>
</div>
As you can see, I have a div, 2 spans and an image. I am using WatiN to try and click the image, that will then expand the menu, exposing yet another layer that I will need to click something else on. The problem I am having is in getting the 'Click' to happen. From what I can see in the snippet, it seems to me I need to be able to click the event, but cannot 'find' it with the code.
Any help out there to be had?
I have also had issues with clicking on certain elements.
I've run into issues where I could only click on an element if it was highlighted by mousing over the element.
Since I cannot see your code snippet, I can't tell if there is any javascript that deals with mouseover associated with the image, but if there is, you can try the following:
img.FireEvent("onmouseover");
img.FireEvent("onmousedown");
img.FireEvent("onMouseup");
You might also might want to try img.FireEvent("onclick") as well.
These are all guesses, since I can't see your code. It's also possible that rather than clicking on the image element itself, that you may want to try clicking on the parent object.
EDIT:
Ok, now that I can see your code, it appears that you should fire an onClick event against the span with the 'onclick' code in it.
I don't see an image listed in your code snippet, but this code should call the parent of the lowest level span.
Watin.Core.Span span = browserinstance
.Span(Find.By("innertext", "Quote Processing"));
span.Parent.FireEvent("onclick");
The DOM content that you intended to post is not visible. You might want to edit your post and check if it is visible.
In order to click on images
Watin.Core.Image img = browserinstance.Image(Find By Constraint);
if (img!=null and img.Exists)
img.ClickNoWait();
OR
img.FireEvent("onclick");

HTML string does not get verified

These spaces are not added by me on HTML SIDE and i cannot edit HTML
I want to know what should my comparison string?
I am using watin to automate website testing process but I am unable to encounter only one button.Every other works
watin searches content by name /values /id and many more and works fine but when i see the value of the submit button that i need to be clicked it has some breaks &nsbp so i think they are playing some role
Here is the html:
<span class='button'><input type="submit" value=" Login " /></span>
<span class='button'><input type="button" value=" Back " onclick="history.back(-1)" /></span>
and here is the code to search
browser.Button(WatiN.Core.Find.ByValue(" Login ")).Click();
what can be done??
-- Suggestion -- (i.e. too big for a comment)
You shouldn't use to add spaces to the submit button. Rather, you should use CSS to style the button to your liking. So you would have something like:
input[type=button] {
padding:10px;
min-width: 150px;
}
By the same token, this could eliminate any of the issues you're having with selecting the button. It could be an issue of encodings breaking with watin and as a result, doing this with CSS will make debugging the issue much cleaner and much easier.
Edit:
Have you tried searching by ID as opposed to by value? ID's are supposed to be unique on a page, so if it doesn't find it by those means, then that's one issue that can be rules out. It could also be the fact that you're searching for a button. A <button> is not the same as a <input type="button">.
Edit 2: Even though the issue was due to encodings breaking, I still recommend you reset that button to reset the text (removing all the non breaking spaces) and attach an id/name to it. The reason being for internationalization purposes - and if for some reason you modify the size of the button in the designer, or i18n the app and the text is different, your test will break.
You shouldn't use entities with WatiN.
This code will work, but you have to use real non-breaking space character:
browser.Button(
WatiN.Core.Find.ByValue(
"   Login   ")).Click();
This is probably inconvenient, but you could use (after adding reference to System.Web) HttpUtility class:
browser.Button(
WatiN.Core.Find.ByValue(
System.Web.HttpUtility.HtmlDecode(
" Login "))).Click();
But, if I were you, I would just go with Regex:
browser.Button(
WatiN.Core.Find.ByValue(
new Regex(#"^\s*Login\s*$"))).Click();
or even new Regex("Login").
Interesting thing: If you ever will have to Find.ByText you don't have to bother so much, and you can use regular space (ie. not exactly non-breaking space). That's because native IE IHTMLElement::getAttribute (http://msdn.microsoft.com/en-us/library/aa752280(VS.85).aspx) converts from innertext attribute to regular spaces, but from value, id etc. it doesn't ( are converted to real non-breaking spaces - 0xA0)
Wow, you really like spaces! I would remove those and use padding/margins like html was designed to be used. Then you wont need all those spaces and you can assign a proper value to your button which watiN will recognize.
I think it is because the in the HTML source is actually an escaped version of the special character that represents a none breaking space. So in you C# source, you'll probably need that character instead of the html entity code. I think you can find the code of that character by using this button to submit a GET form. It will show the escaped character code in the url.
Of course it is better not to put the spaces in there at all. You should give the button a padding using CSS instead.

Categories