I have a method below that selects a random item from a dropdown list. This works well mostly, but on occasion it will reselect the default option which is usually "Please Select" or a blank value.
Im looking at how to avoid selecting the default/already selected option.
Below is my code from my Selenium framework.
public void selectRandomDropDownOption(By dropdownIdentifier, By options) {
// Putting in a loop to specify number of selections and to then select different values every time.
for (int i = 0; i < 1; i++) {
// Click on drop down
clickElement(dropdownIdentifier);
// Get items in dropdown
List<WebElement> items = driver.findElements(options);
// Get size of options
int size = items.size();
// Generate a random number which will be used to select an item in the list
int randNo = ThreadLocalRandom.current().nextInt(0, size);
items.get(randNo).click();
}
}
Example of the DOM (fairly standard)
<select name="IPQ_OA_TFINSUP1" id="IPQ_OA_TFINSUP1" onchange="ul_conditional_display_all('FD', 'OA_TFINSUP1');" required="required" class="sv-form-control" aria-invalid="true" aria-describedby="SVb4935443-ae1b-4cf6-357d-57c92b2da566">
<option value="" selected="selected">Please select</option>
<option value="ELCAS">ELCAS</option>
<option value="PROF_AND_CAREER">Professional and Career Development Loan</option> etc....
</select>