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");
Related
public static WebElement residentialStatus(String name) {
element = LoginPage.driver.findElement(By.xpath("PATH"));
Select oSelect = new Select(element);
oSelect.selectByVisibleText(name);
System.out.println("Residential Status: " + name);
return element;
}
the HTML is:
<select name="residentialStatus">
<option value="">--Please Select--</option>
<option value="B">Boarding</option>
<option value="O">Home Owner</option>
<option value="P">Living with Relative</option>
<option value="R">Renting</option>
In the above code, I am trying to select a Dropdown,
When I am selecting 'Home Owner' it fails (possible because there is a Space), when I choose 'Boarding' or 'Renting' then it works fine.
I am unable to understand where I am going wrong.
(Feeding Data from Excel, so I can't change it to 'selectByArray as all other dropdowns are working fine from the same excel)
How to consider list value as a dropdown value?
How to select Edit Drop down value from List...
<div class="nice-select demoBasic" tabindex="0">
<span class="current">Please select</span>
<ul class="list">
<li class="option" data-value="1">Edit</li>
<li class="option" data-value="2">Delete</li>
</ul>
</div>
I got it solution ...
driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/table/tbody/tr[3]/td[3]/div")).click();
driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
Thread.sleep(4000);
driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/table/tbody/tr[3]/td[3]/div/ul/li[1]")).click();
driver.manage().timeouts().implicitlyWait(6000, TimeUnit.SECONDS);
Thread.sleep(4000);
You should try using By.cssSelector() to get all options in list as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//First click on select div
WebElement select = wait.until(ExepectedConditions.elementToBeClickable(By.cssSelector("div.nice-select.demoBasic")));
select.click();
//Not get all nested options
List<WebElement> options = wait.until(ExepectedConditions.presenceOfNestedElementsLocatedBy(select, By.cssSelector("ul.list li.option")));
//Now iterate to fetch all options and select edit option
for(WebElement option : options)
{
if(option.getText().equals("Edit"))
{
option.click();
}
}
Or you can select an option on the basis of text in one line using By.xpath() as below :-
wait.until(ExepectedConditions.elementToBeClickable(By.xpath(".//li[text() = 'Edit']"))).click();
`<div class="col1">
<strong>
<select name="Category" multiple size="4">
<option value="A">A
<option value="B" selected>B
<option value="C">C
<option value="D">D
</select></strong>
</div>`
I have div class containing a drop down list as given above, i just need the value of 'selected' item from the drop down list using Jsoup
Search for the <select> Element, iterate over it’s children and check if the selected attribute is present:
Document doc = Jsoup.parse("your html")
String selectedVal = null;
Elements options = doc.getElementsByAttributeValue("name", "Category").get(0).children();
for (Element option : options) {
if (option.hasAttr("selected")) {
selectedVal = option.val();
}
}
Or in short with a CSS-like selector:
String selectedVal = doc.select("select[name=Category] option[selected]").val();
Currently working on Selenium WebDriver and using Java for scripting.
I have stored all drop down values of db in property file and want to compare same values whether they are in UI as in DropDown options.
The visualization.txt which is in C: directory contains the below options
visualizationId=Day,Week,Month,Quarter,Semester,Year,RD Tech Group,ICC,Center,Software Pack,Product,Project,Customer PRs,Severity ,Priority.
So How can I compare both values are matching. I need to get all the drop down options from property file i.e visualization.txt then need to check in the drop drop down in UI.
<select id="visualizationId" style="width: 120px; display: none;" name="visualization">
<option value="day">Day</option>
<option value="week">Week</option>
<option selected="" value="month">Month</option>
<option value="quarter">Quarter</option>
<option value="semester">Semester</option>
<option value="year">Year</option>
<option value="techgroup">RD Tech Group</option>
<option value="icc">ICC</option>
<option value="center">Center</option>
<option value="softwarepack">Software Pack</option>
<option value="product">Product</option>
<option value="project">Project</option>
<option value="customer">Customer PRs</option>
<option value="severity">Severity</option>
<option value="priority">Priority</option>
</select>
Try the following piece of code. Should help:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
BufferedReader in = new BufferedReader(new FileReader("C:\\visualization.txt"));
String line;
line = in.readLine();
in.close();
String[] expectedDropDownItemsInArray = line.split("=")[1].split(",");
// Create expected list :: This will contain expected drop-down values
ArrayList expectedDropDownItems = new ArrayList();
for(int i=0; i<expectedDropDownItemsInArray.length; i++)
expectedDropDownItems.add(expectedDropDownItemsInArray[i]);
// Create a webelement for the drop-down
WebElement visualizationDropDownElement = driver.findElement(By.id("visualizationId"));
// Instantiate Select class with the drop-down webelement
Select visualizationDropDown = new Select(visualizationDropDownElement);
// Retrieve all drop-down values and store in actual list
List<WebElement> valuesUnderVisualizationDropDown = visualizationDropDown.getOptions();
List<String> actualDropDownItems = new ArrayList();
for(WebElement value : valuesUnderVisualizationDropDown){
actualDropDownItems.add(value.getText());
}
// Print expected and actual list
System.out.println("expectedDropDownItems : " +expectedDropDownItems);
System.out.println("actualDropDownItems : " +actualDropDownItems);
// Verify both the lists having same size
if(actualDropDownItems.size() != expectedDropDownItems.size())
System.out.println("Property file is NOT updated with the drop-down values");
// Compare expected and actual list
for (int i = 0; i < actualDropDownItems.size(); i++) {
if (!expectedDropDownItems.get(i).equals(actualDropDownItems.get(i)))
System.out.println("Drop-down values are NOT in correct order");
}
//Read the data from property file
String options = property.getProperty("visualizationId");
//read the options from drop down in UI
List<WebElement> dropdownOptions = new Select(driver.findElement(By.id("visualizationId"))).getOptions();
String uiOptions="";
for(WebElement eachOption : dropdownOptions ) {
uiOptions+=eachOption.getText()+",";
if(!options.contains(eachOption.getText())) {
System.out.println(eachOption.getText()+" is present in UI but not in property file");
}
}
for(String eachOptionFromPropertyFile : options.split(",")){
if(!uiOptions.contains(eachOptionFromPropertyFile)) {
System.out.println(eachOptionFromPropertyFile+" is present in property file but not in UI");
}
}
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");