javascript - 如何在 Selenium 中选择 aria-autocomplete 输入字段?

标签 javascript html selenium selenium-webdriver

我遇到了这个自动完成输入文本字段。 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");

下面的截图将显示:

enter image description here

现在如何选择自动完成/建议列表中的第一项?

“选择”元素现在是否填充了所有建议列表项?

我做了类似的事情:

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/

相关文章:

html - 如何自定义博客中的默认时间戳

javascript - 为什么 anchor 不能仅在 Firefox 中使用 map 坐标?

javascript - Selenium WebDriver Click 方法和 Javascript Click 方法之间的区别

javascript - 服务器提供的下拉列表中的 Angular 默认值

javascript - AngularJS:指令限制: 'E' 阻止在 Jasmine 单元测试中调用元素 click() 事件

Javascript注入(inject)覆盖响应式 slider 的CSS

java - 删除 Selenium WebDriver 中的只读属性

javascript - 如何使用 AngularJS 在 Kendo 网格列模板中使用函数

javascript - 获取子 iframe src 值

selenium - 获取 Appium 1.2 上的所有子元素