Not able to select dropdown values using selenium Java

Viewed 159

I am new to automation and currently exploring selenium Java for my application Trying to automate a web application with selenium Java.

I have looked online and can only find answers if the dropdown was 'Select'. Please suggest how i can select a value from the dropdown .

HTML code:

<span title="" class="k-widget k-dropdown k-header innova-invalid" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="" aria-disabled="false" aria-busy="false" aria-activedescendant="32406016-a12b-4ce6-a9f7-56f84a0883bd" style="" xpath="1">
<span unselectable="on" class="k-dropdown-wrap k-state-default" style="">
<span unselectable="on" class="k-input"></span>
<span unselectable="on" class="k-select" aria-label="select"></span>
</span>
<select kendo-dropdownlist="$ctrl.dropdownList" k-data-source="$ctrl.dataSourceOptions.dataSource" k-data-text-field="'Display'" k-data-value-field="$ctrl.valueField" k-value-primitive="$ctrl.valuePrimitive" name="HSRisk" k-options="$ctrl.options" k-ng-model="$ctrl.model" k-rebind="$ctrl.rebindTimestamp" k-ng-disabled="$ctrl.isDisabled" ng-class="{'innova-invalid': $ctrl.hasError}" data-role="dropdownlist" style="display: none;" class="innova-invalid">
<option value="true"></option>
<option value="false"></option>
</select>
</span>
<span unselectable="on" class="k-dropdown-wrap k-state-default" style="">
<span unselectable="on" class="k-input"></span>
<span unselectable="on" class="k-select" aria-label="select">
<span class="k-icon k-i-arrow-60-down"></span>
</span>
</span>
<select kendo-dropdownlist="$ctrl.dropdownList" k-data-source="$ctrl.dataSourceOptions.dataSource" k-data-text-field="'Display'" k-data-value-field="$ctrl.valueField" k-value-primitive="$ctrl.valuePrimitive" name="HSRisk" k-options="$ctrl.options" k-ng-model="$ctrl.model" k-rebind="$ctrl.rebindTimestamp" k-ng-disabled="$ctrl.isDisabled" ng-class="{'innova-invalid': $ctrl.hasError}" data-role="dropdownlist" style="display: none;" class="innova-invalid">
<option value="true">
Yes
</option>
<option value="false">
No
</option>
</select>
</span>

3 Answers

You can't click on options from select drop-downs. You need to handle them differently.

From your code sample I would say that in order to select an option you have to do this:

CODE SAMPLE

Select yourSelectSection = new Select(driver.findElement(By.id("YOUR_ID")));
yourSelectSection.selectByIndex(0); // by using index
yourSelectSection.selectByVisibleText("Yes"); // or by using text

Note: you have to import Select library. If you need to read more about this library you can do it here.

Select yourSelectSection = new Select(driver.findElement(By.Xpath("dropdown-list")));
yourSelectSection.selectByIndex(0); // by using index
yourSelectSection.selectByVisibleText("true");

this will work

Please try the below code:

driver.findElement(By.id(“dropdownField”)).sendKeys(“mention text of required value from dropdown”); //send required option in dropdown field
driver.findElement(By.id(“option1”)).click(); 

Or try with the below code:

List<WebElement> options = driver.findElements(By.xpath(“”));
for(WebElement option : options) {
if (option.getText().contains(“mention text of required value from dropdown”)) {
option.click();
break;
}

Please refer to the link for more details- how to select a particular value in a dropdown without using the methods of Select class in Selenium.

Related