Get ID of a radio button with Selenium CSS selector - c#

I just wanted to know that how can I get the ID of
<input type="radio" class="class1" id="radio1" />
with the help of CSS selectors in Selenium.

This is a more difficult and intriguing question than you might suppose. It is important to be aware that values for the HTML class attribute are not unique. Thus, it is quite possible (and in fact probable) that your HTML page may have multiple elements with the class set to "class1". So selecting strictly on the class will only work if your radio button is the first element on your web page with that class value. Working only with your example, a safer locator expression would be
selenium.GetAttribute("css=input.class1[type='radio']#id");
This matches only class1 elements that are radio buttons. It is much more specific--it correctly avoids prior elements that happen to also have the class1 value for the class attribute. But it is still not satisfactory--again it will match the first such element. You can improve further if, for example, you have more than one group of radio buttons. Say you wanted the first radio button in the second group from this code fragment...
<div id='group1'>
<input type="radio" class="class1" id="radio1" />
...
</div>
<div id='group2'>
<input type="radio" class="class1" id="radio1" />
...
</div>
... you could use a locator like this:
selenium.GetAttribute("css=#group2 .class1[type='radio']#id");
(Note that the space after '#group2' is important!)
However, the above is mostly to explain the problem rather than to provide a good answer, because the fundamental problem remains.
Most likely your code is really something like this, with multiple radio buttons all having the same type and same class, distinguishable only by position:
<input type="radio" class="class1" id="radio1" />
<input type="radio" class="class1" id="radio2" />
<input type="radio" class="class1" id="radio3" />
The only way to target one of these other than the first is by explicit indexing, but that requires you know the index a priori, e.g.:
selenium.GetAttribute("css=#group2 [type='radio']:nth-child(2)#id");
(For more on constructing CSS recipes for Selenium see my article and wallchart XPath, CSS, DOM and Selenium: The Rosetta Stone on Simple-Talk.com.)

I will give you an example:
selenium.GetAttribute("css=.class1#id");
Here is the link for the documentation http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.DefaultSelenium.GetAttribute.html

Related

Grouping radio buttons without name attribute

I've got a form similar to the below, the name attributes have been set with MVC model binding in mind.
At the moment I'm only able to select one radio between the two forms. I want to select a radio button for each form, i.e. one radio button selected per form at any given time. I can't find a way to do this whilst keeping my name attribute naming convention.
Any ideas, thanks.
<div class="form-group-1">
<input type="radio" name="Students[0].Answer1" value=""><br>
<input type="radio" name="Students[0].Answer2" value=""><br>
</div>
<div class="form-group-2">
<input type="radio" name="Students[1].Answer1" value=""><br>
<input type="radio" name="Students[1].Answer2" value=""><br>
</div>

ASP.NEŠ¢ MVC 5 checkbox/radio buttons binding in action

I've searched google/SO for a lot of time now. And I didn't find a decent answer. My question is simple. How can I bind the radio selected button or the checked checkboxes to my action.
NO HTML HELPERS(I think that most people use the radio button and checkbox helpers cuz they don't know actually how they work, and use them by heart).
Simple example:
<input type="checkbox" id="foo" name="foo" value="1" checked>
<input type="checkbox" id="foo2" name="foo" value="2">
<input type="checkbox" id="foo3" name="foo" value="3">
<input type="radio" id="male" name="foo10" value="male">
<input type="radio" id="female" name="foo10" value="female">
I read many examples using an object with IsChecked property inside etc. I just want to use a view model like this in my action:
public class FooViewModel {
public int ChosenRadioButtonId {get;set;}
public List<int> ChosenCheckboxes {get; set;}
}
(PS: I don't know if I am mistaking but those helpers generate flaw html - duplicate ids, and everyone knows the basic HTML principles that one id can be used only ONCE)
the name of the checkboxes should exactly be the same as the name in ViewModel, so try to change the names to ChosenCheckboxes[] if you wanna bind them to a list.

Input data is not returning by passing through object

I am passing the input data from the .cshtml page to the action method.
Here is my .cshtml page:
#model passingdata
<form asp-controller="Home" method="post" asp-action="About" >
<button class="btn-danger" type="submit" value="Submit"></button>
<div class="form-group">
<label>
<input asp-for="date" placeholder="Date" class="col-md-8" />
</label>
<label>
Select from these Four Classes
<input type="radio" name="class" asp-for="classselect1" id="classselect1" value="classselect1" class="col-md-4" /> <p> #Model.classname1</p>
<input type="radio" name="class" asp-for="classselect2" id="classselect2" value="classselect2" class="col-md-4" /> <p> #Model.classname2</p>
<input type="radio" name="class" asp-for="classselect3" id="classselect3" value="classselect3" class="col-md-4" /> <p> #Model.classname3</p>
<input type="radio" name="class" asp-for="classselect4" id="classselect4" value="classselect4" class="col-md-4" /> <p> #Model.classname4</p>
</label>
</div>
</form>
And here is my controller code which is invoked when i Click on the button.
[HttpPost]
public IActionResult About(passingdata p)
{
ViewData["Message"] = "Your application description page.";
Teacher.classselect1 = p.classselect1;
Teacher.classselect2 = p.classselect2;
Teacher.classselect3 = p.classselect3;
Teacher.classselect4 = p.classselect4;
Teacher.date = p.date;
return View();
}
The input data like date and bool value from the radiobutton is not passing through the object of the class which contain these variables.
Please help me in this.
If i remember correctly, the Name attribute of each radiobutton is how .net MVC will map the values to your model.
In this case, all of your names are "class", which essentially means you have 1 form field named class with 4 options.
I would recommend using the html helper classes, because they will automatically create the proper html for you. The answer to this post should help: When using .net MVC RadioButtonFor(), how do you group so only one selection can be made?
If you dont want to use the helper just remember that when you submit a form, the data that is posted is based on the name of each form field. .Net does some magic in the background to serialize your Model for you, but essentially you are just submitting data in the format "?prop1=val1&prop2=val2".
Update
I figure maybe I should clarify a little better why what you are doing is not working how you expect.
When you post or put data via a form, it passes the input fields (text box, radio button, checkbox, etc...) as either querystring params or are part of the body. Radio buttons work a little differently than other input type. For a radio button, there are multiple input elements, but only one of them is valid. That is handled by using the name attribute. In your case, all of the names are "class", which means that the only thing being passed to the server is a single "?class={val}" (val is the value of which ever radio button is selected).
If your passingdata model had a property called "class", it would be populated. If your goal is to populated all 4 of the classselect properties with different values, you would need the name of each radio button to be different. But if there was only one radio button with each name, then each property could only have 1 value. You would need multiple RadioButtons with the same name to have multiple values (only one of which is selectable for each property).
Hopefully that clarifies what is wrong and gets you in the right direction.

How to have Google Custom Search V2 Search element on master page but result on a Child Page

I am trying to Implement the Google CSE in my site, I have formatted the look and feel of the Search box and result on google CSE site, now I want to do the following:
Have the search box on Master page so it shows up every where
Show results on a seperate page say "search.aspx", which is a child page of the Master
On Results page the Search box remain where it is in master page. and there is a div for it to show results.
I know we have following elements that can be used.
<gcse:search>
<gcse:searchbox> and <gcse:searchresults> A two-column layout
<gcse:searchbox-only> A standalone search box
<gcse:searchresults-only> A standalone block of search results.
But i suppose my requirement can only be met by using a combination of above, but not sure which one.
If any one has done this, can you please guide me what is the way to go.
a trick to that is to make a redirect to the search page placing the search on the url, eg something like:
You search everwhere (on master) for the word test, and your code if you are not on search.aspx is make a redirect to:
search.aspx?q=test
Now, inside the search.aspx, you read the query q and place it on the text box that google use to make search and that's all - ah, and one post back to google.
eg the code will looks like:
<form action="search.aspx" id="Form1">
<div>
<input type="hidden" name="cx" value="partner-pub-XXXXXXXXXX" />
<input type="hidden" name="cof" value="FORID:10" />
<input type="hidden" name="ie" value="UTF-8" />
<input type="text" name="q" size="46" value="<%=Server.HtmlEncode(Current.Request["q"]) %>" />
<input type="submit" name="sa" value="Search" />
</div>
</form>

What is the easiest way to check all form values to see if they are empty C#?

I have a form with four values. If any of them are empty I want to alert the user. Would the best way be to just have 4 separate if...then statements? or is there some sort of a fancy thing that I can do with C# to accomplish this?
thanks!
code
The form in question is a static HTML form:
<form id="form1" action="launch.aspx" method="post" target="_blank" name="form1">
<input type="hidden" name="ClientID" value="123456" />
<input type="hidden" name="Password" value="986574321" />
<input type="hidden" name="PracType" value="001" />
<input type="hidden" name="Encrypt" value="11258746345" />
</form>
Request.Form returns a NameValueCollection. You can loop through that collection and check if the value is set, if it's not then you can return the name of the missing field.
You can do this in C# on the .aspx.cs page, but I think Kev, is right. The RequiredFieldValidator is the quickest, simplest way to get the desired behavior you want.
By the way, why are all your form items hidden? Are we to assume you're using the control? If you can supply us with some more information, I think we can answer your question more accurately.

Categories