我试图使用Selenium从下拉元素中选择一个项目,但唯一的唯一ID是数据绑定值。我尝试过使用
<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>
然后我想选择一个选项值。我已经尝试了以下代码的两部分无济于事:
customerProvinceField = browser.find_element_by_css_selector("value: customerProvince, options: availableProvinces, optionsText: 'Name', optionsCaption: ''")
customerProvinceField = browser.find_element_by_css_selector("value: customerProvince")
两者都收到错误:“消息:无效的选择器:指定了无效或非法的选择器”。
答案 0 :(得分:0)
使用以下: -
customerProvinceField= driver.find_element_by_xpath('//select[contains(@data-bind,"value: customerProvince, options: availableProvinces, optionsText:")]')
代码就像
Select(driver.find_element_by_xpath('//select[contains(@data-bind,"value: customerProvince, options: availableProvinces, optionsText:")]')).select_by_value('Nunavut').click()
参考: -
Selecting a value from a drop-down option using selenium python
希望它能帮到你