How can I select an option from a dropdown menu in selenide in a java test?

81 Views Asked by At

I need to develope a java test with the use of selenide, navigating through an online platform automatically and I couldn't find out how to select an option from a certain dropdown list. I've tried several code. However using Select dropdown = new Select(driver.findElement(By.id("id_of_the_dropdown_list"))); as a base for the code did not work, i have then tried:

SelenideElement menueGesamt = $(By.id("id_of_the_dropdown_list"));
        ElementsCollection optionen = menueGesamt.findAll(By.cssSelector("option"));
        int maxRandom = optionen.size();
        int random = new Random().nextInt(maxRandom);
        SelenideElement gewaehlteOption = optionen.get(random);
        String optionValue = gewaehlteOption.getAttribute("value");
        menueGesamt.selectOptionByValue(optionValue);

But this also didn't work. There was no error but the option was just not selected. The image shows what should happen, the option should be selected and the dropdown menu should close afterwards.

enter image description here

1

There are 1 best solutions below

1
On

Can you show more information please? Your HTML like this? Has "option value"?

<select id="dropdownListId">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>

If your HTML is not like this then Selenide will not work.

You need use method click() for select element of dropdown list