selenium - Unable to select dropdown with search box in selenium
问题描述
Dropdown box HTML:
<span class="select2-selection select2-selection--single" role="combobox"
aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-country-container">
<span class="select2-selection__rendered" id="select2-country-container" title=""
</span>
<span class="select2-selection__arrow" role="presentation"><b role="presentation"></b>
</span>
</span>
For dropdown option Denmark=>
<li class="select2-results__option select2-results__option--highlighted" role="treeitem" aria-selected="false">Denmark</li>
I am unable to select the dropdown by Select class as there no Select is present. How to select the dropdown and how to search in the searchbox ?
解决方案
您必须单击下拉菜单以展开它,然后单击您希望选择的选项,使用 XPath 或 CSS 等选择器,而不是使用选择类。
以下是我Denmark
从您提供的下拉列表中选择选项的方法:
# click combobox to expand it
driver.find_element_by_xpath("//span[@role='combobox']").click()
# wait for "Denmark" option to appear
denmark_option = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//li[text()='Denmark']")))
# select the "Denmark" option
denmark_option.click()
推荐阅读
- uwp - 在 Windows UWP 中访问更高的 UDP 端口
- ios - 如何让 Web 检查器在 iOS 13.3.1 和 Safari 13.0.5 或 Safari Technology Preview 13.2 上运行?
- node.js - AWS Elastic Beanstalk | 错误:无法运行 npm 安装。快照日志以获取更多详细信息
- c# - C# 中的动态浮点格式化程序
- c# - 没有可用的测试..通过 exe 在 VM 上使用构建工具和测试代理进行测试
- python - 播放带有 winsound 问题的随机 wav 文件
- html - 媒体查询无法在 Opera mini 浏览器上运行,但在 Firefox 和 chrome 浏览器上运行良好
- azure - 是否可以使用 onPremisesSyncEnabled(Microsoft Graph API)来监视 Azure AD 是否同步?
- node.js - 为什么会出现“0 - 未知错误 [object ProgressEvent]”错误?
- c# - 按组在单独的线程中运行处理