我正在尝试从Selenium Java的下拉列表中选择一个项目
HTML
<div class="aui-ss aui-ss-has-entity-icon" id="issueTypeKey-single-select" data-query=""><input autocomplete="off" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="false" class="text aui-ss-field ajs-dirty-warning-exempt" id="issueTypeKey-field" type="text"><div class="ajs-layer-placeholder"><div class="ajs-layer box-shadow" aria-hidden="true" style="width: 53px; position: absolute; left: 759px; top: 302px; max-height: 328px; display: none;"><div class="aui-list" id="issueTypeKey-suggestions" tabindex="-1" role="listbox" style="display: block;"><div class="aui-list-scroll" tabindex="-1" role="presentation"><ul class="aui-last"><li class="aui-list-item aui-list-item-li-purchase active" role="option" id="purchase-1"><a class="aui-list-item-link aui-iconised-link" role="presentation" href="#"><img class="icon" alt="" src="/servicedesk/issue-type-icons?icon=purchase">Purchase</a></li><li class="aui-list-item aui-list-item-li-fault" role="option" id="fault-2"><a class="aui-list-item-link aui-iconised-link" role="presentation" href="#"><img class="icon" alt="" src="/servicedesk/issue-type-icons?icon=fault">Fault</a></li><li class="aui-list-item aui-list-item-li-access" role="option" id="access-3"><a class="aui-list-item-link aui-iconised-link" role="presentation" href="#"><img class="icon" alt="" src="/servicedesk/issue-type-icons?icon=access">Access</a></li></ul></div></div></div></div><span class="icon aui-ss-icon noloading drop-menu"><span>More</span></span><img class="aui-ss-entity-icon" alt="" src="/servicedesk/issue-type-icons?icon=it-help"></div>
<input autocomplete="off" role="combobox" aria-autocomplete="list" aria-haspopup="true" aria-expanded="false" class="text aui-ss-field ajs-dirty-warning-exempt" id="issueTypeKey-field" type="text" aria-controls="issueTypeKey-suggestions">
<div class="aui-list" id="issueTypeKey-suggestions" tabindex="-1" role="listbox"></div>
<span class="icon aui-ss-icon noloading drop-menu"><span>More</span></span>
</div>
<select class="select imagebacked aui-ss-select" data-edit-value="" name="issueTypeId" id="issueTypeKey" multiple="multiple" style="display: none;"><option value="10000" style="background-image: url(/servicedesk/issue-type-icons?icon=it-help);">IT Help</option><option value="10001" style="background-image: url(/servicedesk/issue-type-icons?icon=purchase);">Purchase</option><option value="10002" style="background-image: url(/servicedesk/issue-type-icons?icon=fault);">Fault</option><option value="10003" style="background-image: url(/servicedesk/issue-type-icons?icon=access);">Access</option></select>
</td>
<td class="request-type-description"><textarea class="textarea-cell" name="description"></textarea></td>
<td class="request-type-actions"><div><input type="submit" class="aui-button aui-button-primary create-request-type-button" value="Create request type" resolved=""></div></td>
Java selenium
Select select=new Select(driver.findElement(By.xpath("//div[@id='issueTypeKey-single-select']/")));
select.selectByVisibleText("Fault");
我想要选择的选项名为&#34;故障&#34;。
请注意,我不能使用&#34;选择&#34;功能,因为我的HTML是&#34;输入&#34;
有什么建议吗?
答案 0 :(得分:0)
查看Selenium WebDriver - Filling In Forms documentation。而且你的身份。 issueTypeKey-single-select
不在HTML中 - 您可能想要访问issueTypeKey
。这有display: none;
,与
Select select=new Select(driver.findElement(By.xpath("//div[@id='issueTypeKey']")));
select.deselectAll();
select.selectByVisibleText("Fault");
答案 1 :(得分:0)
我之前必须处理一些非本地选择元素,并且只需单击“输入”即可获得好运。然后在弹出选项后单击该选项。请注意,您可能需要等待选项作为元素位置可见,并且显示选择下拉选项的页面会创建竞争条件。此外,当你的代码中有一个id时,它只是直接选择它而不是使用XPath来选择id。试试下面的例子:
WebElement input = driver.findElement(By.id("issueTypeKey-field"));
input.click();
WebElement fault = input.findElement(By.linkText("Fault"));
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(fault))
.click();
首先,驱动程序点击ID为issueTypeKey-field
的元素。这应该会提出选择选项。通过创建Web驱动程序等待对象并使用预期条件,您等待链接文本所在的元素为其true
方法返回isDisplayed
。一旦从等待返回,它将被点击。 10
设置等待将继续尝试检查元素是否可见的超时(以秒为单位)。如果达到超时且元素不可见,则会抛出TimeoutException
。该示例将WebElements保存为稍后要使用的变量,我通常不喜欢(由于StaleElementReferenceExceptions可能取决于页面),但在这种情况下,您将要限制搜索{{1}输入中的元素。