0

I am attempting to select an item from a drop-down element using Selenium but the only unique ID is in the data-bind value. I have tried using

<select data-bind="value: customerProvince, options: availableProvinces, optionsText: 'Name', optionsCaption: ''" class="form-control uc-not-searchable" size="1">
<option value=""></option><option value="">Alberta</option><option value="">British Columbia</option><option value="">Manitoba</option><option value="">New Brunswick</option><option value="">Newfoundland and Labrador</option><option value="">Northwest Territories</option><option value="">Nova Scotia</option><option value="">Nunavut</option><option value="">Ontario</option><option value="">Prince Edward Island</option><option value="">Quebec</option><option value="">Saskatchewan</option><option value="">Yukon Territory</option>
                        </select>

I would then like to select an option value. I have tried both pieces of the following code to no avail:

customerProvinceField = browser.find_element_by_css_selector("value: customerProvince, options: availableProvinces, optionsText: 'Name', optionsCaption: ''")

customerProvinceField = browser.find_element_by_css_selector("value: customerProvince")

Both receive an error of: "Message: invalid selector: An invalid or illegal selector was specified".

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
user8472879
  • 11
  • 1
  • 2

1 Answers1

0

Use below :-

customerProvinceField= driver.find_element_by_xpath('//select[contains(@data-bind,"value: customerProvince, options: availableProvinces, optionsText:")]')

Code will be like

Select(driver.find_element_by_xpath('//select[contains(@data-bind,"value: customerProvince, options: availableProvinces, optionsText:")]')).select_by_value('Nunavut').click()

refer :-

Selecting a value from a drop-down option using selenium python

Hope it will help you

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125