How to select the value from dropdown box in selenium webdriver - java

Below having my code.I need to select the option values like value1,value2..
<td class="column-label" width="50%">
<div id="divRawMaterials" name="Companies" style="position: relative">
<select name="yieldStandardDTO.rawMaterialName" size="1" onmouseover="dropdowntooltip(this);" onmouseout="return nd();"
nmousedown="showtooltip(this);" onchange="chooseYieldItems('name');" style="width:205px;" class="select"><option value="">-- Select --</option>
<option value="010080159">value1</option>
<option value="010080024">value2</option>
<option value="010080071">value3</option>
<option value="010030014">value4</option>
<option value="010090009">value5</option>
And
I tried below methods but it doesn't work.Pls help me out of this.
METHOD:1
Select dropdown = new Select(driver.findElement(By.name("yieldStandardDTO.rawMaterialName")));
System.out.println(dropdown);
//dropdown.selectByVisibleText("value1");
//dropdown.selectByIndex(1);
dropdown.selectByValue("value1");
METHOD:2
WebElement selectMonth = driver.findElement(By.xpath("//div[#id='divRawMaterials']/select"));
List<WebElement> options = selectMonth.findElements(By.tagName("value1"));
for (WebElement option : options) {
if("Alphonso Mango".equals(option.getText()))
option.click();
METHOD:3
WebElement hiddenWebElement =driver.findElement(By.xpath("//div[#id='divRawMaterials']/select"));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",hiddenWebElement);

For the drop down to appear you can try the following :
((JavascriptExecutor)driver).executeScript("$('select[name=\"yieldStandardDTO.rawMaterialName\"]').click();");
Now that the drop down is visible, you can use :
Select dropdown = new Select(driver.findElement(By.name("yieldStandardDTO.rawMaterialName")));
dropdown.selectByVisibleText("value1");

Related

Select menu element with Selenium

I would like to select an element of a dropdown menu using Selenium in a Java application.
I tried several times using Select, List < WebElement > etc... with no success.
The html of the webpage is this:
<div class="margin-top_2">
<span class="right_column_2">
<div id="f1:idSelectTipoDoc" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all ui-helper-clearfix" style="vertical-align: middle; width: 198px;">
<div class="ui-helper-hidden-accessible">
<select id="f1:idSelectTipoDoc_input" name="f1:idSelectTipoDoc_input" style="vertical-align:middle">
<option value="">Select a fruit</option><option value="A">Apple</option>
<option value="T">Tangerine</option></select>
</div>
<input type="text" name="f1:idSelectTipoDoc_editableInput" class="ui-selectonemenu-label ui-inputfield ui-corner-all" tabindex="-1" style="cursor: pointer; width: 182px;">
<div class="ui-selectonemenu-trigger ui-state-default ui-corner-right"><span class="ui-icon ui-icon-triangle-1-s"></span></div></div></span>
</div>
This was my last try but the element in dropdown menu was not selected:
//open the dropdown menu
WebElement tipo1 = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"f1:idSelectTipoDoc\"]/div[2]")));
tipo1.click();
// select the Apple line
WebElement tipo2 = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id=\"f1:idSelectTipoDoc\"]")));
Select elem = new Select(tipo2);
elem.selectByVisibleText("Apple");
Anyone know why it is not working? Thanks
your locator is wrong, you are using locator for div element and not select. use:
WebElement tipo2 = new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[#id=\"f1:idSelectTipoDoc_input\"]")));
Select elem = new Select(tipo2);
elem.selectByVisibleText("Apple");

Read option text from drop down values in selenium

List<WebElement> statusvalues = driver.findElements(By.id("ddlStatus"));
for (WebElement option : statusvalues)
{
System.out.println(option.getText());
}
This is the script I have used to write.
System not providing the error but I didn't get the result. There are five drop down values I need to write in the output. The HTML code is given below.
<select id="ddlStatus" name="Status" class="full-width" data-role="dropdownlist" style="display: none;">
<option value="" selected="selected"> -- Select -- </option>
<option value="11">Arts</option>
<option value="13">Science</option>
<option value="14">Engineering</option>
<option value="64">Law</option>
<option value="85">Teaching</option>
<option value="87">Journalist</option>
</select>
Just exploring the selenium webdriver and how to write the drop down values in the output.
List<WebElement> statusvalues = driver.findElements(By.id("ddlStatus"));
it's an element and not collection of elements so you need to do:
WebElement selectElement = driver.findElement(By.id("ddlStatus"));
In that element you have options so you can make collection:
List<WebElement> options = selectElement.findElements(By.tagName("option"));
and now you can loop...
Try:
List<WebElement> statusvalues = driver.findElement(By.id("ddlStatus")).findElements(By.tagName("option"));
for (WebElement option : statusvalues)
{
System.out.println(option.getText());
}
For the best way you should try using Select for drop down as below :-
Select sel = new Select(driver.findElement(By.id("ddlStatus")));
List<WebElement> options = sel.getOptions();
for (WebElement option : options)
{
System.out.println(option.getText());
}
Note:- driver.findElements(By.id("ddlStatus")); only provides you the list of all Elements which has id ddlStatus so in this case you could get only the drop down element here not it's option.
Hope it will help you..:)
Also you can try this
List <WebElement> options = driver.findElements(By.xpath("//*[#id='ddlStatus']/option"));
for(WebElement option : options)
{
System.out.println(option.getText());
}
You can also handle the dropdowns in 3 other ways as follows,
new Select(driver.findElement(By.id("ddlStatus"))).selectByVisibleText("Particular Text");
new Select(driver.findElement(By.id("ddlStatus"))).selectByIndex(Particular index);
new Select(driver.findElement(By.id("ddlStatus"))).selectByValue("Particular value");

Element cannot locate with the text- selenium webdriver

Currently working on selenium webdriver and the using the language Java.
Log.info("Clicking on To weekrange dropdown");
JavascriptExecutor executor25 = (JavascriptExecutor)driver;
executor25.executeScript("document.getElementById('toWeekYear).style.display='block';");
Select select25 = new Select(driver.findElement(By.id("toWeekYear")));
select25.selectByVisibleText("2011");
JavascriptExecutor executor26 = (JavascriptExecutor)driver;
executor26.executeScript("document.getElementById('toWeekYear).style.display='block';");
Select select26 = new Select(driver.findElement(By.id(" toWeek")));
select26.selectByVisibleText(" W 5");
Thread.sleep(6000);
In the above code, am trying to select the week range and it is in the form to select year-2011 and week-W 5. This values are selecting from the dropdown. The problem is while selecting 1st dropdown it is selecting the year and while trying to select the second drop down am getting the error Cannot locate element with text: W 1
Here is the HTML tag:
<select id="fromWeekYear" style="width:60px" name="fromWeekYear">
<option value="2010"> 2010</option>
<option value="2011"> 2011</option>
<option value="2012"> 2012</option>
<option selected="" value="2013"> 2013</option>
</select>
<select id="fromWeek" style="width:60px" name="fromWeek">
<option value="1"> W 1</option>
<option value="2"> W 2</option>
<option value="3"> W 3</option>
<option value="4"> W 4</option>
<option value="5"> W 5</option>
and
<select id="toWeekYear" style="width:60px" name="toWeekYear">
<option value="2010"> 2010</option>
<option value="2011"> 2011</option>
<option value="2012"> 2012</option>
<option selected="" value="2013"> 2013</option>
</select>
<select id="toWeek" style="width:60px" name="toWeek">
<option value="1"> W 1</option>
<option value="2"> W 2</option>
<option value="3"> W 3</option>
<option value="4"> W 4</option>
<option value="5"> W 5</option>
Here is my fromweek javascript:
Log.info("Clicking on From weekrange dropdown");
JavascriptExecutor executor23 = (JavascriptExecutor)driver;
executor23.executeScript("document.getElementById('fromWeekYear').style.display='block';");
Select select23 = new Select(driver.findElement(By.id("fromWeekYear")));
select23.selectByVisibleText("2011");
JavascriptExecutor executor24 = (JavascriptExecutor)driver;
Thread.sleep(6000);
executor24.executeScript("document.getElementById('fromWeek').style.display='block';");
Select select24 = new Select(driver.findElement(By.id("fromWeek")));
select24.selectByVisibleText(" W 1");
Thread.sleep(6000);
Try this:
new Select(driver.findElement(By.id("fromWeekYear")).selectByValue("2010");
new Select(driver.findElement(By.id("toWeek")).selectByValue("1");
Value is single, use value.
If by text, your text has space:
new Select(driver.findElement(By.id("fromWeekYear")).selectByVisibleText(" 2010");
new Select(driver.findElement(By.id("toWeek")).selectByVisibleText(" W 1");
Other issue is that maybe your space is (& nbsp;)
You have some spacing issues:
driver.findElement(By.id(" toWeek")));
Should not have a space in the id:
driver.findElement(By.id("toWeek")));

Retrieving the Selected multiple select values

I need to have multiple values selected when I call that LoadDropDown function. But its not working.... Please suggest how to get multiple options selected.
<td>Select Profession:</td>
<td>
<select name="selectcontent_profession" id="selectcontent_profession" multiple>
<option value="0">None</option>
<option value="10">Student</option>
<option value="201">Lecturer</option>
<option value="333">Head Of Department</option>
<option value="421">Principal</option>
<option value="523">Chairman</option>
<option value="667">Management</option>
<option value="784">Placement Officer</option>
</select>
</td>
<%
String s1= "10,333,421,523";
String[] array = s1.split(",");
%>
<script >
function LoadDropDown()
{
<%for(int i=0;i<array.length;i++){%>
document.getElementById("selectcontent_profession").value ="<%= array[i]%>";
<%}%>
}
</script>
First you have to declare the select's multiple selection option as follows:
<select name="selectcontent_profession" id="selectcontent_profession" multiple="multiple">
And your function should be like this:
var fld = document.getElementById('selectcontent_profession');
for(var i=0;i<fld.options.length;i++){
for(var j=0;j<array1.length;j++){
if(fld.options[i].value==array1[j])fld.options[i].selected=true;
}
}
You are trying to get the value of the dropdown whereas you have to set it to selected.
You have to add the options like
var option = document.createElement("option");
option.text = "<%= array[i]%>";
option.value = "<%= array[i]%>";
document.getElementById("selectcontent_profession").options
.add(option);
document.getElementById("selectcontent_profession").options[<%=array[i]%>].defaultSelected =true;
Use this statement in the loop.

How to check that the options in dropdown are displayed twice in Selenium Webdriver using Java

I am stuck in one logic, where I have to verify if the options in dropdown are displaying twice. I searched in google for the solution, but didnt find any.
I have this code to get all the options from the dropdown. But not really sure how should I check if the options are displayed twice.
new Select(driver.findElement(By.xpath(//*[#id='unmappedTech']))).selectByVisibleText(VisibleText);
new Select(driver.findElement(By.xpath(//*[#id='unmappedTech']))).getOptions();
In my application, options are displaying twice in dropdown. Here is the source code of dropdown:
<table><tbody><tr>
<td>
<select name="unmappedTech" id="unmappedTech" multiple="multiple" size="10" style="width: 160px;">
<option class=" firepath-matching-node" value="142">Cloud Service Assurance</option>
<option value="123">Cloud Service Assurance Zenoss for Data Center and Cloud</option>
<option value="6">CUSTOMER COLLABORATION</option>
<option value="12">DESKTOP VIRTUALIZATION</option>
<option value="13">FACILITIES</option>
<option value="7">INSTANT MESSAGING</option>
<option value="8">MOBILE COLLABORATION</option>
<option value="141">Network Address Translation</option>
<option value="15">NETWORKING</option>
<option value="3">SECURITY</option>
<option value="16">STORAGE</option>
<option value="81">TestTechnology_Dont_Delete</option>
<option value="10">UNIFIED COMMUNICATIONS</option>
<option value="20">VCH VIDEO</option>
<option value="17">VIRTUALIZATION And CONSOLIDATION</option>
<option value="21">VtechnologyVtechnologyVtechnologyVtechnology</option>
<option value="2">WIRELESS</option>
<option class=" firepath-matching-node" value="142">Cloud Service Assurance</option>
<option value="123">Cloud Service Assurance Zenoss for Data Center and Cloud</option>
<option value="6">CUSTOMER COLLABORATION</option>
<option value="12">DESKTOP VIRTUALIZATION</option>
<option value="13">FACILITIES</option>
<option value="7">INSTANT MESSAGING</option>
<option value="8">MOBILE COLLABORATION</option>
<option value="141">Network Address Translation</option>
<option value="15">NETWORKING</option>
<option value="3">SECURITY</option>
<option value="16">STORAGE</option>
<option value="81">TestTechnology_Dont_Delete</option>
<option value="10">UNIFIED COMMUNICATIONS</option>
<option value="20">VCH VIDEO</option>
<option value="17">VIRTUALIZATION And CONSOLIDATION</option>
<option value="21">VtechnologyVtechnologyVtechnologyVtechnology</option>
<option value="2">WIRELESS</option>
</select>
</td>
I am not a Java person, so forgive me, but you are essentially just wanting to loop through all the options from that Select, keep a record of them, and ensure that one each loop iteration, that option doesn't already exist, so pseudo-code:
Select selectElement = new Select(driver.findElement(By.xpath(//*[#id='unmappedTech'])));
ArrayList<string> options = new ArrayList<string>();
for (WebElement element in selectElement.getOptions()) {
if (options.contains(element.getText())) {
// do something that lets the test fail because the option is listed twice
}
options.add(element.getText());
}
This isn't a Selenium problem as such, it's just comparing a list to see if something is already contained within that list.
This is the java code. Essentially checking whether duplicate is found by rechecking a set.
List<WebElement> options = driver.findElement(
By.xpath("//*[#id='unmappedTech']")).findElements(
By.tagName("option"));
HashSet<String> optionNames = new HashSet<>();
for (WebElement option : options) {
if (optionNames.contains(option.getText()))
System.out.println("Duplicate found");
else
optionNames.add(option.getText());
}
Select s = new Select(driver.findElement(
By.xpath("//Select[#id='unmappedTech']")));
List<WebElement> list = s.getOptions();
Set<String> listNames = new HashSet<String>(list.size());
for (WebElement element : list) {
//Set will not allow to add duplicate value
if(listNames.add(element.getText())==false){
System.out.println("Duplicate value is: "+element.getText());
}
}

Categories