0

Currently, I am facing the issue of selecting a random value from the drop-down list, but I don't want to select the placeholder value in Selenium Java. HTML Can you please help me select a random value from the list, but I don't want to select placeholder value 0. Thanks in advance.

I have tried with blow xpath, but sometimes it selects a placeholder value as well. //*[@id='ddlEnquirySource']/div/ul/li

Ash
  • 3
  • 3
  • Please read: https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors and https://stackoverflow.com/help/minimal-reproducible-example – Conal Tuohy Mar 07 '23 at 12:35

1 Answers1

0

Combine random number:

public static int getRandomNumber(int min, int max) {
    return (int) ((Math.random() * (max - min)) + min);
}

with selectByIndex:

WebElement selectElement = driver.findElement(By.Id("ddlEnquirySource"));
List<WebElmenet> selectOptions = selectElement.findElements(By.TagName("option"));
Select mySelect = new Select(selectElement);
mySelect.selectByIndex(getRandomNumber(1, selectOptions.size()));
pburgr
  • 1,722
  • 1
  • 11
  • 26