Java HtmlUnit select option without name or id - java

How do i select the option Speedserver with HtmlUnit ?
It has no id or name so i dont know how to select it.
I read something about xPath but how do i use it ?
Here is the html code:
<select name="server" id="serverselector">
<option value="">Server</option>
<option value='s1.'>Server 1</option>
<option value='server2.'>Server 2</option>
<option value='speed.'>Speedserver</option>
</select>

OK, use getElementByID(#serverselector) to get HtmlElement by ID, then use:
getElementsByTagName("option") on it to get all the HtmlElements
with option tag and go through them with
getTextContent("Speedserver") and check the text content to be matched.
or, use getElementsByAttribute("option", "value", "speed") on it.

Related

how can get dropdown selected value in textbox in jsp

i want selected value in input text field in jsp how to do please help? I am new with jsp.
<select class="leavetype" name="leavetype">
<option value="10">CL</option>
<option value="7">ML</option>
<option value="12">SL</option>
<option value="20">EL/PL</option>
<option value="5">CPL</option>
</select>
<input type="text" name="leave"/>
See this video:- https://www.youtube.com/watch?v=89rRD04QZE4
Hope so it will help you.
In jsp page you may write. ${param.leavetype}.
You can achieve through javascript by using onChange function. Within in function need to do like this document.getElementById ("leave").value = data.value;
Refer this Answer

Get all select Values in Java

I have a SELECT tag in JSP page that contains few values :
<select name="selectNumber" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
And I want to get all that values in a Java Class.
What I have tried that :
HttpServletRequest req = (HttpServletRequest)ServletActionContext.getRequest();
String[] selectedNumbers = req.getParameterValues("selectNumber");
But that Array only contains the value I am selecting, and If I don't select any value it contains null. But I want all the option values of the select Tag in java class. Any help?
I am using Struts 2 framework.
if you want to send all the Options to Your Servlet then you must first select them all using simply using Jquery
Before Submitting the Form
$("#selectNumber option").prop('selected',true);
Note #is used for Id , which i prefer if you need to use only name then try
$('[name="selectNumber"]');

Add one more option field in the Struts2 select tag

i have one struts 2 select tag like below.
<s:select list="myList" listKey="myListVal" listValue="myListDesc"></s:select>
it rendered as below
<select data-inputs="myListInput" >
<option value="myListVal1" >myListDesc1</option>
<option value="myListVal2" >myListDesc2</option>
<option value="myListVal3" >myListDesc3</option>
</select>
i want to add one more option field with null value
<option value="" ></option>
to the Option field with modification in the list.
In struts 1 it is possible like below
<html:option value=""></html:option>
like that is it possible in the struts 2
note:i couldn't able to modifiy the list before the page load in the java
There is an Attribute called emptyOption in the select tag.
It worked as below.
<s:select list="myList" listKey="myListVal" listValue="myListDesc" emptyOption="true" ></s:select>
if you want to add one more option field with null value at the begining of the list you can use headerKey & headerValue attribute of s:select tag.
Check example given below.
<s:select list="myList" listKey="myListVal" listValue="myListDesc" headerKey="0" headerValue="" ></s:select>
So the first element of your list will be empty & it's value will be 0(zero) here.
It will be render as
<select data-inputs="myListInput" >
<option value="" >0</option>
<option value="myListVal1" >myListDesc1</option>
<option value="myListVal2" >myListDesc2</option>
<option value="myListVal3" >myListDesc3</option>
</select>
You can set any value on place of 0 (zero).
Hope this will help you.

How to avoid the duplicate entry of select box option while edit page?

Could anyone please explain me how to avoid the duplicate enters of records in to the select box options during editing the page.I am using java to develop web application I am very new to that,I need to remove the duplicates from the select box option while editing.Could anyone please help me .
Sample Code
<td><select property="employeestatus" value="" id="employeestatus">
<c:if test="${employee.employeestatus!=null}">
<option value="${employee.employeestatus}">${employee.employeestatus}</option>
</c:if>
<option value="">Select</option>
<option value="Active">Active</option>
<option value="Terminated">Terminated</option>
<option value="Deceased">Deceased</option>
<option value="Resigned">Resigned</option>
<option value="InActive">InActive</option>
</select>
<html:errors property="employeestatus"/>
<p id="employeestatus" style="color:red"/>
</td>
I would suggest to add the selected attribute on the option corresponding to your "employeestatus" variable instead of duplicating this option on the first line.
See HTML option selected Attribute
By the <html:errors ...>, I gess you use Struts 1. If it's true, I would use the
<html:select ...> tag and
<html:options or optionsCollection ...>
with java Map store in some context (servlet, session or request) to configure options. That struts tag will select the good option corresponding to your variable value.
See Struts <html:select ...> doc

How to check from which optgroup an option is picked from <select> html feild

i am using struts tag with . there are two opt groups with same keys but different values. when i submit my form, keys go to server to save. now on server side i want to check with which optgroup it was associated how can i do it ?
following is html code
<optgroup label="Group1">
<option value="1">opt1</option>
<option value="3">opt2</option>
<option value="4">opt3</option>
</optgroup>
<optgroup label="Group2">
<option value="1">opt4</option>
<option value="3">opt5</option>
<option value="4">opt6</option>
</optgroup>
Use different values, such as:
<option value="grp1_1">opt1</option>
...
<option value="grp2_1">opt1</option>
Then parse out the value on the server after the for is submitted.

Categories