0

I have a little problem selecting options out of drop down lists with selenide (java).

Here's a little snippet of the HTML code and my try to select the option by the value:

HTML snippet

[Java code]

    String dateRangeSearchFor = "YESTERDAY";
    ElementsCollection ListOfOptions = $(By.id("searchMaskForm:jobSearch_dateRange_input")).$$(By.tagName("option"));
    logger.info("selecting option");
    for (SelenideElement listElement : ListOfOptions)
    {
        String valueOfElement = listElement.getAttribute("value");
        if (valueOfElement.equals(dateRangeSearchFor))
        {
            //$(By.xpath("//*[@id='searchMaskForm:jobSearch_dateRange_input']/option[contains(., '"+dateRangeSearchFor+"')]")).setSelected(true);

            listElement.setSelected(true); break;

        }
    }

For some reason the code is not working, neither with the text nor with the index. Any suggestions?

Edit: .click(); and selectOption(); aren't working neither

Mo Joe
  • 13
  • 1
  • 4

5 Answers5

2

The piece of code below will help:

String dateRangeSearchFor = "YESTERDAY";
Select select = new 
Select($(By.id("searchMaskForm:jobSearch_dateRange_input")));
select.selectByValue(dateRangeSearchFor);

In my case it did.

BTW, if the automation test suite you're creating is a part of automation, that includes functional and load testing, this link will help you to combine those tools in one system, check it out - How to automate Selenium and jmeter testing .

Michal
  • 443
  • 1
  • 10
  • 25
2

SelenideElement have method selectOptionByValue(java.lang.String... value)

Kartoch
  • 7,610
  • 9
  • 40
  • 68
Aleks Be
  • 63
  • 1
  • 11
1

Selenide provides the following methods for a selecting an option in dropdownlist.

  • selectOptionByValue(value)
  • selectOption(text)
  • selectOption(index)
  • selectOptionContainingText(text)
0

If you know the index of the element in dropdown menu then you can just use built-in methon selectOption().

It will look like this:

$(CSS-selector).selectOption(index-of-element);

!!REMINDER: CSS-selector MUST point to the <select> element in HTML.

0

<select class="switch-version" id="switch-version-select" onchange="switchVersionSelect()">
        <option value="/v1.x/demo/my_boss_is_in_a_hurry/flexigrid">Flexigrid</option>
        <option value="/v1.x/demo/my_boss_is_in_a_hurry/datatables">Datatables</option>
        <option value="/v1.x/demo/my_boss_is_in_a_hurry/bootstrap" selected="selected">Bootstrap V3 Theme</option>
        <option value="/v1.x/demo/my_boss_is_in_a_hurry/bootstrap-v4">Bootstrap V4 Theme</option>
        <option value="/v1.x/demo/my_boss_is_in_a_hurry/bootstrap-v5">Bootstrap V5 Theme</option>
    </select>

In the example of the html code above, to select one of the value options with Selenide simply implement the code below.

public class HomePage {

SelenideElement bootstrapElement = $(By.cssSelector("#switch-version-select"));

public void selecionarDropDown(){

        $(bootstrapElement).selectOptionContainingText("Bootstrap V4 Theme");

        }
    }