【问题标题】:Selenium (Python) selecting an option within a nested multiselect optgroupSelenium (Python) 在嵌套的多选 optgroup 中选择一个选项
【发布时间】:2019-03-27 01:29:21
【问题描述】:
我有一个嵌套的多选。如何在 Selenium 中选择一个选项(使用 python)?这就是我正在尝试的...
filter_brands = Select( filter_panel.find_element_by_css_selector("select.form-control#filter-brands[name='filter_brands']") )
filter_brands.deselect_all()
filter_brands.select_by_value("ABC") #doesnt work!
我想我需要选择 optgroup#filter-brands-optgroup,但我不能使用 Select 来做到这一点(Select 必须选择 select 元素)
【问题讨论】:
标签:
python
selenium
xpath
css-selectors
optgroup
【解决方案1】:
尝试根据可见文本进行选择,
filter_brands.select_by_visible_text("ABC")
【解决方案2】:
要从 optgroup 中选择 文本为 ABC 的 选项,您可以使用以下任一解决方案:
-
css_selector:
filter_panel.find_element_by_css_selector("select.form-control#filter-brands[name='filter_brands']>optgroup#filter-brands-optgroup option[value='ABC']").click()
-
xpath:
filter_panel.find_element_by_xpath("//select[@class='form-control' and @id='filter-brands'][@name='filter_brands']/optgroup[@id='filter-brands-optgroup']//option[@value='ABC']").click()