JeBo
JeBo

Reputation: 197

How to select a drop-down menu value wihtout an id with Selenium using Python?

I need to select an element/item from a drop-down menu that doesn't have an id element using Python and Selenium.

The piece of HTML code:

<mbo-transaction-mode-select mode="filters.mode" class="ng-isolate-scope">
    <select class="form-control input-sm ng-pristine ng-untouched ng-valid ng-empty" ng-model="mode" ng-options="value for (key,value) in vm.modes">
        <option value="" class="" selected="selected"></option>
        <option label="LIVE" value="string:LIVE">LIVE</option>
        <option label="TEST" value="string:TEST">TEST</option>
    </select>

The current option I found on Stackoverflow or Google used the Select method, but that option used find_element_by_id which I unfortunately don't have. I tried to use:

select = Select(browser.find_element_by_xpath("//input[@ng-model='mode']"))
select.select_by_visible_text('LIVE')

But this gave the error:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[@ng-model='mode']

Is there another way for me the select the dropdown and one of its options?

Upvotes: 1

Views: 398

Answers (1)

Evgeniy Chiruk
Evgeniy Chiruk

Reputation: 358

You need to fix your xpath, as here:

element = browser.find_element_by_xpath("//select[@ng-model='mode']")
driver.execute_script("arguments[0].scrollIntoView();", element)
select = Select(element)
select.select_by_visible_text('LIVE')

Upvotes: 3

Related Questions