Selenium WebDriver选择组合框项目? [英] Selenium WebDriver to select combo-box item?

查看:100
本文介绍了Selenium WebDriver选择组合框项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Selenium WebDriver和JBehave在我们的网络应用上运行集成测试。我有一个方法将值输入表单输入。

We are using Selenium WebDriver and JBehave to run "integration" tests on our web-app. I have a method that will enter a value into a form input.

@When("I enter $elementId value $value")
public void enterElementText(final String elementId, final String value) {
    final WebElement webElement = webdriver.findElement(By.id(elementId));
    webElement.clear();
    webElement.sendKeys(value);
}

但是当我尝试使用它来选择下拉列表中的项目时列出它(不出所料)失败

But when I try to use this to select an item in a drop-down list it (unsurprisingly) fails


java.lang.UnsupportedOperationException:您只能设置作为输入元素的
元素的值

java.lang.UnsupportedOperationException: You may only set the value of elements that are input elements

如何在组合中选择一个值?

How do I select a value in the combo?

推荐答案

这是怎么做的:

@When("I select $elementId value $value")
public void selectComboValue(final String elementId, final String value) {
    final Select selectBox = new Select(web.findElement(By.id(elementId)));
    selectBox.selectByValue(value);
}

这篇关于Selenium WebDriver选择组合框项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆