How do Parse the select-menu HTML? using Java - java

How do we parse below HTML , and get KEY - VALUE separately
<select id="secondary_contact_type" style="width: 150px" onblur="validateField_userContactDetails_SecContactType(this,true)" name="userContactDetails.SecContactType">
<option value="-1"> Select </option>
<option value="1">Self</option>
<option value="2">CA</option>
<option value="3">ERI</option>
<option value="4">TRP</option>
<option value="5">Others</option>
</select>
i used the below code,
Elements options=doc.select("[id=secondary_contact_type]");
for(Element data:options){
System.out.println(data.attr("value"));
System.out.println(data.text());
}
But it is giving only KEY , in one String value, like
Select Self CA ERI TRP Others

You should also select option.
Try this.
Document doc = Jsoup.parse(html);
Elements options=doc.select("select[id=secondary_contact_type] > option");
for(Element data:options){
System.out.println(data.attr("value"));
System.out.println(data.ownText());
}

Related

Create URL based on user input

I am very new to Java and will like to learn it by applying in my work.
I will like to generate a link based on user input via drop down list.
https://example.com/"input1"/testing_"input2"_xx.html
Input 1 drop down list
Input 2 is date
How do I concatenate input 1 and 2 into the URL?
You have to get the values of the input fields:
function generateURL()
{
var part1 = document.getElementById('part1').value;
var part2 = document.getElementById('part2').value;
var url = "https://example.com/"+part1+"/"+part2+".html";
document.getElementById('result').innerHTML = url;
}
Part 1 of URL:<br/>
<select id="part1">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select>
<br/>
Part 2 of URL:<br/>
<select id="part2">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select>
<br/>
<button onClick="generateURL();">Generate URL</button>
<br/>
URL Generated:
<span id="result"></span>
In place of the second dropdown select you can use anything from date to input box.
UPDATE
A date example:
function generateURLX()
{
var partx = document.getElementById('partx').value;
var party = document.getElementById('party').value;
var urlx = "https://example.com/"+partx+"/"+party+".html";
document.getElementById('resultx').innerHTML = urlx;
}
Part 1 of URL:<br/>
<select id="partx">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
</select>
<br/>
Part 2 of URL:<br/>
<input type="date" id="party" placeholde="Date">
<br/>
<button onClick="generateURLX();">Generate URL</button>
<br/>
URL Generated:
<span id="resultx"></span>

Cannot count/print all options listed under listbox using selenium Webdriver

I have this snippet. I'm trying to count/print all options listed under country listbox.
<select id="country" class="text-box drop-down-list ng-pristine ng-not-empty ng-valid ng-valid-required ng-touched" ng-class="{'text-box-error': form.country.$touched && form.country.$invalid}" ng-options="country.name as country.name for country in countries" ng-model="mv.country" name="country" required="" style="">
<option translate="CUSTOMER_SUPPORT_COUNTRY_SELECT" value="" selected="selected">Select One</option>
<option label="Afghanistan" value="string:Afghanistan">Afghanistan</option>
<option label="Aland Islands" value="string:Aland Islands">Aland Islands</option>
<option label="Albania" value="string:Albania">Albania</option>
<option label="Algeria" value="string:Algeria">Algeria</option>
<option label="American Samoa" value="string:American Samoa">American Samoa</option>
<option label="Andorra" value="string:Andorra">Andorra</option>
<option label="Angola" value="string:Angola">Angola</option>
<option label="Anguilla" value="string:Anguilla">Anguilla</option>
<option label="Antarctica" value="string:Antarctica">Antarctica</option>
<option label="Antigua and Barbuda" value="string:Antigua and Barbuda">Antigua and Barbuda</option>
<option label="Argentina" value="string:Argentina">Argentina</option>
I am getting count as a '1' though list box has many.
WebElement X = driver.findElement(By.id("country"));
Select s=new Select(X);
List<WebElement> elementCount = s.getOptions();
int itemSize = elementCount.size();`enter code here`
System.out.println(itemSize);
for(int i = 0; i < itemSize ; i++){
String optionsValue = elementCount.get(i).getText();
System.out.println(optionsValue);
Thanks #guy,your suggestion had solved my issue.
I have added wait statement -thread.sleep() after select statement & it works.
WebElement X = driver.findElement(By.id("country"));
Select s=new Select(X);
List<WebElement> elementCount = s.getOptions();
Thread.sleep(2000);
int itemSize = elementCount.size();
/* System.out.println(itemSize);
for(int i = 0; i < itemSize ; i++){
String optionsValue = elementCount.get(i).getText();
System.out.println(optionsValue);
When we are using ng-options in select tag, options need to be load from java script. So, Elements will get loaded but options will take some time to load from java script. It can be avoided by using some wait time.

Refreshing JSP select list value on Page refresh

I have below code in my jsp :
<select name="productId" id ="productId" onchange="getDetail()">
<% for (int i = 0;i < no_rows;i++){
%>
<option value="<%=ar[i]%>"><%=ar[i]%></option>
<%
}
%>
</select>
On page refresh(F5), the values in the drop down is not getting refreshed.The previously selected value remain there. DB call to fetch the detail is in JSP itself.
How to refresh the values?
Try JSTL <c:forEach> Tag instead
<select name="productId" id ="productId" onchange="getDetail()">
<c:forEach var="item" items=${yourListNameStoredInSomeScope}>
<option value="{item}">${item}<option>
</c:forEach>
</select>
try something like this:
<select name="productId" id ="productId" onchange="getDetail()">
<option value=" " selected></option>
<% for (int i = 0;i < no_rows;i++){
%>
<option value="<%=ar[i]%>"><%=ar[i]%></option>
<%
}
%>
</select>
if you use selected attribute , when the drop down is loaded, the pre-selected option is the default in your case " ".

How do I verify all the available options in a dropdown using Java and Webdriver

I have a dropdown with many values. I want to write a method where I can enter the expected text of all the dropdown values and compare it with the actual text of the all the dropdown values. If they match, then the tests passes. I am currently using webdriver and Java.
<select id="ctl00_cphMainContent_dq14_response" name="ctl00$cphMainContent$dq14$response">
<option value="0" selected="selected">Please Select...</option>
<option value="253">DEP900</option>
<option value="252">DEP800</option>
<option value="251">DEP700</option>
<option value="250">DEP600</option>
<option value="248">DEP400</option>
<option value="247">DEP300</option>
<option value="246">DEP200</option>
<option value="245">DEP100</option>
<option value="249">DEP500</option>
<option value="254">DEP1000</option>
Try this
public boolean checkOptions(String[] expected){
WebElement select = driver.findElement(By.id("ctl00_cphMainContent_dq14_response"));
List<WebElement> options = select.findElement(By.xpath(".//option"));
int k = 0;
for (WebElement opt : options){
if (!opt.getText().equals(expected[k]){
return false;
}
k = k + 1;
}
return true;
}

iterating the list in JSTL

I have the controller file which holds a list of values in the model like,
List caseIds = new ArrayList();
for(int i=0; i<caseLists.size(); i++)
{
if(caseIds.contains(caseLists.get(i).getCaseId()))
continue;
else
caseIds.add(caseLists.get(i).getCaseId());
}
model.put("caseIdList", caseIds);
and i want to show this caseIds in UI in a dropdown box. My code looks like this,
<select name="caseIds" id="caseIds">
<option value="">All</option>
<c:forEach var="item" items="${model.caseIdList}">
<option value="<c:out value='${item.caseIdList}'/>" >
<c:out value="${item.caseIdList}"/>
</option>
</c:forEach>
</select>
I mean I want the dropdown(select box) with an id and value as caseId's. I understand that there is an error in getting item with item.caseIdList. How should I approach?
item in the for loop in the jsp will be the caseId that was added with caseLists.get(i).getCaseId(), so I think you just want the following:
<select name="caseIds" id="caseIds">
<option value="">All</option>
<c:forEach var="item" items="${model.caseIdList}">
<option value="<c:out value='${item}'/>" >
<c:out value="${item}"/>
</option>
</c:forEach>
</select>
This is what you want to do :
<select name="caseIds" id="caseIds">
<option value="">All</option>
<c:forEach var="item" items="${model.caseIdList}">
<option value="<c:out value='${item}'/>" >
<c:out value="${item}"/>
</option>
</c:forEach>
</select>
You need to use item instead of item.caseIdList since the caseIdList contains Integer.

Categories

Resources