Michael Liew
Michael Liew

Reputation: 15

Selenium Web Driver Selecting option from Drop down

I've got this HTML

<div>
      <div tabindex="0" class="dropdown xs-dropdown show" id="selBudgetYearRange-wrap" style="border: 1px solid rgb(206, 212, 218); border-image: none;">
        <button tabindex="-1" class="btn btn-default dropdown-toggle" id="selBudgetYearRange-btn" aria-expanded="true" aria-haspopup="true" type="button" data-toggle="dropdown">2020 vs 2019 (Oct 2019 - Mar 2020 vs Oct 2018 - Mar 2019)
        </button>
        <div class="dropdown-menu show" id="selBudgetYearRange-dmenu" aria-labelledby="selBudgetYearRange-btn" style="left: 0px; top: 0px; width: calc(100% + 2 * 1px); margin-top: calc(0px + 1px); margin-left: calc(-0px - 1px); position: absolute; transform: translate3d(0px, 21px, 0px);" x-placement="bottom-start">
        <a class="dropdown-item active" onclick="dropdownChanged(this, 'selBudgetYearRange','201910-202003,2');;return false;" href="javascript:void(0)">2020 vs 2019 (Oct 2019 - Mar 2020 vs Oct 2018 - Mar 2019)</a>
        <a class="dropdown-item" onclick="dropdownChanged(this, 'selBudgetYearRange','201810-201909,2');;return false;" href="javascript:void(0)">2019 vs 2018 (Whole Year Oct - Sep)</a>
        <a class="dropdown-item" onclick="dropdownChanged(this, 'selBudgetYearRange','201710-201809,2');;return false;" href="javascript:void(0)">2018 vs 2017 (Whole Year Oct - Sep)</a>
      </div><input name="selBudgetYearRange" id="selBudgetYearRange" type="hidden" value="201910-202003,2"></div>
</div>

which has 3 options

2020 vs 2019 (Oct 2019 - Mar 2020 vs Oct 2018 - Mar 2019)

2019 vs 2018 (Whole Year Oct - Sep)

2018 vs 2017 (Whole Year Oct - Sep)

I've managed to work out that by using

.FindElementByXPath("//*[text()='2019 vs 2018 (Whole Year Oct - Sep)']").Click

I can select the middle option, however it only works if I click on the drop down first so that the options are visible.

I can't seem to get selenium to click on the drop down for me and make the options visible so that I can use the above xpath to select my desired option.

Please can someone advise on how to do this? Or if there's a way of selecting my options without needing to get the drop down to appear first?

Upvotes: 0

Views: 149

Answers (1)

Michael Liew
Michael Liew

Reputation: 15

So managed to get it to work. by using clickdouble (after figuring out the xpath)

I have no idea why I need to use clickdouble instead of click.

But it works, so I'm not going to complain too much

Upvotes: 0

Related Questions