我遇到了这个自动完成输入文本字段。 HTML 是:
<div id="customfield_11930-single-select" class="aui-ss ajax-ss long-field" data-query="35mm Capture - 2.7.1">
<input id="customfield_11930-field" class="text aui-ss-field ajs-dirty-warning-exempt" type="text" autocomplete="off" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="false" aria-busy="false">
<div class="ajs-layer-placeholder">
<span class="icon aui-ss-icon drop-menu noloading">
<span>More</span>
</span>
</div>
<select id="customfield_11930" class="single-select long-field hidden js-sprint-picker aui-ss-select" data-saved-state="" data-saved-id="" data-container-class="long-field" name="customfield_11930" style="display: none;" multiple="multiple">
<option value="" selected="selected"> </option>
</select>
我对这个 HTML 的理解是:
input(customfield_11930-field)
是用户输入的文本域
div(ajs-layer-placeholder)
存储所有自动完成/建议
span()
是用户点击会弹出自动完成/建议列表的地方
select(customfield_11930)
是显示自动完成/建议的地方
所以如果我使用代码:
myDriver.findElement(By.id("customfield_11930-field")).sendKeys("35mm Capture - 2.7.1");
下面的截图将显示:
现在如何选择自动完成/建议列表中的第一项?
“选择”元素现在是否填充了所有建议列表项?
我做了类似的事情:
new Select(myDriver.findElement(By.id("customfield_11930"))).selectByVisibleText("35mm Capture - 2.7.1");
但它不起作用。
其实我对这种自动完成选择菜单的工作原理很困惑,它似乎比普通的下拉列表复杂得多。
任何人的任何解释?谢谢,
最佳答案
文本输入后有一个新的动态字段,id 为 suggestions
您可以通过 css 选择器点击第一个建议 #suggestions > li:nth-child(1)
.你可以看到下面的代码:
python :
driver.find_element_by_css_selector("#customfield_11930-field").clear()
driver.find_element_by_css_selector("#customfield_11930-field").send_keys("35mm Capture - 2.7.1")
driver.find_element_by_css_selector("#suggestions > li:nth-child(1)").click()
Java:
driver.findElement(By.cssSelector("#customfield_11930-field")).clear();
driver.findElement(By.cssSelector("#customfield_11930-field")).sendKeys("35mm Capture - 2.7.1");
driver.findElement(By.cssSelector("#suggestions > li:nth-child(1)")).click();
关于javascript - 如何在 Selenium 中选择 aria-autocomplete 输入字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32988061/