使用databind value在Selenium中选择下拉选项

2024-10-06 06:56:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Selenium从下拉元素中选择一个项目,但是唯一唯一的ID是在数据绑定值中。我试过用

<select data-bind="value: customerProvince, options: availableProvinces, optionsText: 'Name', optionsCaption: ''" class="form-control uc-not-searchable" size="1">
^{pr2}$

然后我想选择一个选项值。我尝试了以下两种代码都没有用:

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

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

两者都收到错误:“消息:无效选择器:指定了无效或非法的选择器”。在


Tags: namebrowserbyvalue选择器elementfindselector
1条回答
网友
1楼 · 发布于 2024-10-06 06:56:54

使用如下:

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

代码应该是

^{pr2}$

参考:-

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

希望对你有帮助

相关问题 更多 >